-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Generic type constraint for ngMocks.stub tedious to write #166
Comments
Hi, it provides instances. In your case it should be like that: {
provide: MyService,
useValue: ngMocks.stub(MockService(MyService), { serviceMethod: ... })
}
|
Yeah you're right, I will edit the comment. Read along, the point is about typings |
Might you share the test you have for better understanding of MyService structure and its role in the test? |
I have a component test and I'm stubbing the service.
Basically what I need is a variant of stub that doesn't let me override methods which are not defined in |
Thanks, got it, I'll take a look on this / next weekend to find a proper solution with the idea to ensure it works correctly with spies and mocks from jasmine and jest. |
Maybe the difference is that I am setting up spies separately for each test and use |
If you set them separately I think you can leave
|
I am setting spies separately, but not for all the methods in all the tests. To be precise, I mock the service and provide empty responses for all the observables, so that whatever a component is doing, won't crash because of .subscribe to undefined. Then:
As for your examples of using |
Are they methods or properties? if they are methods it should be like The problem with stub of methods if that not always Spy or Mock fits exactly signature of the method, and to facilitate it |
In this particular example they're just fields. But in other cases they can be methods or properties.
Thus option B, provide a variant that is more constrained... |
Hi again, might you check if this behavior fits your expectations, you can use |
Sorry for the late reply. I checked your modification (now included in master) and it's not doing what I needed. Type definition currently is:
This constrains overrides a little, but not to the extent that specifying the generic type arguments does. Case in point:
compiles just fine, even though serializeUrl1 is not defined in Router, whereas:
gives a compile-time error. In real life, this happens when Router is a service in your code and somebody renames one of its methods, I want the compiler to complain that I'm mocking a method that's no longer present. I'm not sure how you can do this without breaking backwards compatibility, therefore I suggested a stubExact method that does this. |
Hi again, that's fine, might you try changes in #203? |
Yup, those changes are doing exactly what I need. |
great, will be released on Sunday. |
If I understand it correctly, to stub a service MyService and provide some stubbed implementations, I should be using something like this:
Problem is, the type definition of stub is too relaxed:
This means
overrides
parameter has no relation with type I. Consequently, if I rename serviceMethod later, the test still compiles, which is something I do not like. Most common case is to stub a service and override some of its methods; actually I find it pretty hard to find a use case when I would override the service implementation with different methods.Proposal:
A) constrain "stub" to
Partial<I>
. This breaks backwards compatibility, so I guess it's not desirable.B) add an overload that is more constrained:
Thoughts? I can send a pull request with the change, should be a small one.
Edit:
Right now I'm specifying the generic types explicitly, which is pretty verbose when the service name is a bit longer:
The text was updated successfully, but these errors were encountered: