A. You cannot spy on service methods before mocking dependencies
B. The service method findAll should be mocked on the dependency, not service
C. jest.spyOn syntax is incorrect here
D. Missing async keyword on test function
Solution
Step 1: Identify what to mock in unit tests
In unit tests, you mock dependencies of the service, not the service's own methods.
Step 2: Understand why mocking service method is wrong
Mocking service.findAll replaces the method under test, defeating the purpose of testing it.
Final Answer:
The service method findAll should be mocked on the dependency, not service -> Option B
Quick Check:
Mock dependencies, not the service itself [OK]
Hint: Mock dependencies, never the service method you test [OK]
Common Mistakes:
Mocking the service method instead of its dependencies
Confusing spyOn usage with mocking dependencies
Ignoring async test function requirements
5. You want to test a service method that calls two different repository methods internally. How should you set up your unit test to isolate the service logic correctly?
hard
A. Mock both repository methods with expected return values and inject them into the test module
B. Call the real repository methods to test the full flow
C. Mock only one repository method and leave the other real
D. Do not mock anything and test the service directly
Solution
Step 1: Identify dependencies to mock
Since the service calls two repository methods, both should be mocked to isolate service logic.
Step 2: Setup test module with mocks
Provide mock implementations for both methods in the test module providers to control their outputs.
Step 3: Avoid real calls in unit tests
Calling real repository methods would make it an integration test, not a unit test.
Final Answer:
Mock both repository methods with expected return values and inject them into the test module -> Option A
Quick Check:
Mock all dependencies to isolate service logic [OK]
Hint: Mock all called dependencies to isolate service logic [OK]
Common Mistakes:
Mocking only some dependencies causing partial isolation
Calling real methods making it integration test
Not injecting mocks properly into test module
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.