Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor parameters not being injected #10

Open
Shawboy278 opened this issue Aug 10, 2021 · 0 comments
Open

Constructor parameters not being injected #10

Shawboy278 opened this issue Aug 10, 2021 · 0 comments

Comments

@Shawboy278
Copy link

Hello,

I've been using your example of abstract classes and I'm experincing what seems like constructor parameters not being injected/passed along. The following is a modified version of the example you have posted. I'm inexperienced with Deno/Typescript enough to be uncertain if this is a bug or something I'm misunderstanding of how the language works.

Thank you for any insight you can provide.

import "https://cdn.skypack.dev/@abraham/[email protected]";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Service, ServiceCollection } from "https://deno.land/x/di/mod.ts";

abstract class IA {
    abstract foo(): string;
}

abstract class IB {
    abstract IndirectFoo(): string;
    abstract bar(): string;
}

@Service()
class A implements IA {
    public foo(): string {
        return "foo";
    }
}

@Service()
class B implements IB {
    constructor(
        private a: IA
    ) {}

    public IndirectFoo(): string {
        return this.a.foo();
    }

    public bar(): string {
        return "bar";
    }
}

const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(IA, A);
serviceCollection.addTransient(IB, B);

const ib = serviceCollection.get(IB);

Deno.test("Is expected type", () => {
    assert(ib instanceof B);
});

Deno.test("Expects 'bar'", () => {
    assert(ib.bar() == "bar");
});

Deno.test("Expects 'foo'", () => {
    assert(ib.IndirectFoo() == "foo");
});

Error message:

Check file:///Users/jason/Code/example/example.ts
running 3 tests from file:///Users/jason/Code/example/example.ts
test Is expected type ... ok (6ms)
test Expects 'bar' ... ok (4ms)
test Expects 'foo' ... FAILED (9ms)

failures:

Expects 'foo'
TypeError: Cannot read property 'foo' of undefined
    at B.IndirectFoo (file:///Users/jason/Code/example/example.ts:28:23)
    at file:///Users/jason/Code/example/example.ts:51:15
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:35:15)
    at resourceSanitizer (deno:runtime/js/40_testing.js:72:13)
    at exitSanitizer (deno:runtime/js/40_testing.js:99:15)
    at runTest (deno:runtime/js/40_testing.js:215:13)
    at Object.runTests (deno:runtime/js/40_testing.js:283:28)
    at async file:///Users/jason/Code/example/832dba8c-7c32-4f9d-a6da-f1050ff07807$deno$test.js:4:7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant