-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
mock with a private field failed #8
Comments
What is the use case for this? Why do you have a class only with private fields that you want to mock? You should only test publically exposed functionality. |
He doesn't. He distilled the example, to simplify it for us.
Thats what hes trying to do... class Foo {
private field: string
public bar() {}
}
let mock: Foo = Substitute.for<Foo>() // error TS2322: The problem is he typed the mock to Foo, use the provided generic or let TS infer & it works fine: ObjectSubstitute<OmitProxyMethods<Example>, Example>; User error. |
@joshribakoff yes,I just simplify the test case! class A{
public needMock:Foo
} the field needMock will be injected by some other code...I cant wait TS infer it! |
Ah I see! Yes, that should be resolved as soon as possible! I'll look into it sometime next week, as I am quite busy lately. In the meanwhile, PRs are welcome! |
I made a branch now called You can follow up on the progress there. However, I don't have much time lately. Feel free to fork that branch and continue. |
We can't fix this without breaking other things, until this feature is implemented: |
@ffMathy - I really like this library, but without this fixed, using this library for Angular with its dependency injection is basically a non-starter. For instance, I have a class that depends on a service that I want to mock:
Mocking LocaleService (for usage in initializing your class) will throw a TS error, since Any suggestions? I'm happy to help fix this with a pull request, if you have a suggestion. |
Can you provide a minimal example to reproduce it? Perhaps a PR that adds a unit test or similar? Or a separate project that demonstrates the problem? |
I haven't tested it, but I think the code below provides a minimal example. class ClassA {
constructor(){}
methodA(): string {
return 'abc'
}
}
class ClassB {
constructor(
private classA: ClassA
) {}
methodB(): string {
return this.classA.methodA();
}
methodB2(): string {
return 'def'
}
}
class ClassC {
constructor(
private classB: ClassB
) {}
methodC(): string {
return this.classB.methodB2();
}
}
const classBMock = Substitute.for<ClassB>();
const classC = new ClassC(classBMock); Typescript will complain about that last line because |
Will investigate soon! |
@ffMathy - I created a basic example, using your code. |
Fixed. Thanks for making the library better! |
when I do this:
I get :error TS2322: Type 'ObjectSubstitute<Pick<Foo, never>, Foo>' is not assignable to type 'Foo'.
Property 'field' is missing in type 'ObjectSubstitute<Pick<Foo, never>, Foo>'.
when I change "private" to "public", its ok!
The text was updated successfully, but these errors were encountered: