Issue
I have to cover test-branch and I need to use two stubs, so how can I achieve that?
I tried to set myStubOne in TestBed...
TestBed.configureTestingModule({
providers: [
{ provide: MyService, useValue myStubOne }
]
}).compileComponents()
and then update it in my singletest:
it("should work", () => {
Testbed.ovverideProvider(MyService, myStubTwo)
})
...but it doesn't work. What should I do when i want to use two different stubs in two seperated tests?
Solution
I think you might have a couple of options. First, the call to the service could mockImplementationOnce() multiple times, and each return therefore has different values. Your tests in the same file are run in order, so you can arrange the returns as required.
The mock implementation could use parameters (if used in the original calls) to simply return data as needed as well. The resolveOnce, etc., could also help.
As Jest runs tests in parallel, and you are using a stub for the service to test your code works correctly, you could also separate your tests in well name files. This would allow them to run in parallel (improve speed of testing) and still group like tests together.
Another option is to simply have the 2 blocks in the same file with the full beforeEach in 2 different describe blocks.
Answered By - Steven Scott
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.