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

Add HeroesComponent example with mocked service provider (#110) #188

Merged
merged 4 commits into from
Aug 28, 2018

Conversation

ParkerM
Copy link
Contributor

@ParkerM ParkerM commented Aug 26, 2018

Related to Issue #110
An example of how to mock a service provider's return values using jest.fn() and rxjs' Subject without having to bother with mocking low-level HTTP responses.
I placed the component in its own heroes folder which is kind of awkward since the Hero model and service are in the more generic service folder, but I didn't want to take too many liberties in moving stuff around.

Other small changes:

  • Removed 'rxjs' from the import-blacklist as it was causing linter warnings on existing code.
  • Fixed incorrect describe name on existing HeroService spec.

@thymikee
Copy link
Owner

Honestly I'm against this kind of testing. You mock the service, so whenever you break it, test won't catch it 🤷‍♂️. I usually advice to mock request library/API itself, because that's what your code doesn't have control over – you can however mock request to return anything and check whether your service reacts properly.

@ParkerM
Copy link
Contributor Author

ParkerM commented Aug 27, 2018

Yeah for sure, this type of mocking can be dangerous if the service itself is not also tested. This is under the assumption that the service itself is also properly tested (i.e. via HTTP mocks).

Ideally the component should not know or care where the service is getting its data from or what format it comes in. By mocking the low-level HTTP responses in the component spec the tests become tightly coupled with dependencies that the component itself does not know exist. This can be especially problematic for services that are used in many places. For example, a small change to an external API could require updates to every spec with the service in its dependency chain. However, if the contract between the app and the external API is solely owned by the respective service then only that single spec would need to be updated and the developers can go fast forever.

Another advantage of this style (especially with angular stuff) is that other distant dependencies can be omitted from the spec, which can minimize the required resources and speed up run time.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 28, 2018

Actually what @ParkerM did is from Angular guide for unit test. I also do the same with my work. What @thymikee suggested wasn't wrong but it's more about integration test between the component and the real service without mocking.

So if in the situation of unit test, I'd say mocking like this is ok. But in practical, ofc we need one test to cover the service itself and one more integration test for the component and the service.

@thymikee
Copy link
Owner

Yea, I'm a fan of testing integrations not units :D But I don't see a reason we shouldn't merge that, it's just an example :)

@thymikee thymikee merged commit 20a4c39 into thymikee:master Aug 28, 2018
@ParkerM ParkerM deleted the mock-provider branch August 30, 2018 23:35
@ahmedifhaam
Copy link

Can you update this same example to work by automocking a service class ?

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

Successfully merging this pull request may close these issues.

4 participants