I am using ng-mocks for mocking and so far everything works OK with a few exceptions of some quirks.
I'm using autoSpy to spy every method automatically so I don't have to manually spy on functions. So according to the documentation, I've got, in my test.ts
:
ngMocks.autoSpy('jasmine');
And I can use the spy method to test. For example, I've got something like this:
// in the main beforeEach() while setting up TestBed
myService: MyService = MockService(MyService);
describe(`When button A is clicked`, () => {
beforeEach(() => {
//button click code here
});
it(`Should call functionA one time`, () => {
expect(myService.functionA).toHaveBeenCalled(); // This works ok
});
});
// Further down in the same file
describe(`When button B is clicked`, () => {
beforeEach(() => {
//button click code here
ngMocks.reset(); // I don't think this does what I think it does
});
it(`Should NOT call functionA`, () => {
expect(myService.functionA).not.toHaveBeenCalled(); // This DOES NOT work.
});
});
The second time around, I'm trying to test that the function is not called with a different button, but the spy counts the previous call and fails. If I run just this test with jit
, then it passes. Or if I move this test case above the first one, the first then it works. I can't just do mySpy.calls.reset()
because I haven't assigned a spy manually. I tried myService.functionA.calls.reset()
but there's an error - functoinA
doesn't have calls
method as it's not recognised as a spy.
Furthermore, if I put a debugger just before my expect.not.toHaveBeenCalled()
and check through chrome dev window, I can run this myService.functionA.calls.reset()
on the console and then it works fine.
How do I reset the all spy calls in ng-mocks
please?