I have the following angular service and its jasmine test. The test calls f1() and spies on f2(). The function f2 takes variable v2 and modifies it (sets field a to 3). The function f2 should be called with v2 (as declared in f1) but my test fails on toHaveBeenCalledWith and says actual call was with the object as after the f2 function call. Does jasmine match the parameters for .toHaveBeenCalledWith after the function call, which shouldn't be the recommended way or am I making some mistake here.
Service:
export class JasmineTestClass{
constructor(){
}
f2(v2){
v2.a = 3
};
f1(v1){
let v2 = {
a:30
};
this.f2(v2);
}
}
Test:
describe('Test', () => {
let service: JasmineTestClass;
beforeEach(() => {
service = new JasmineTestClass();
spyOn(service, 'f2').and.callThrough();
});
let v1 = {
a:2, b:3
};
let v2 = {
a:30
};
it('should succeed', () => {
service.f1(v1);
expect(service.f2).toHaveBeenCalledWith(v2); //this is failing
});
})
Log:
Test should succeed FAILED
Expected spy f2 to have been called with [Object ({a:30})] but actual calls were [Object ({a:3})]
Please note that I did debug in Chrome while testing and the function f2() is called with v2 = {a:30}.
expect
s on my spy objecct, I just declare them a the top level of the jasmine test and initialize them every time in thebeforeEach
block. This makes it possible to expect different cases in different tests for the "same" spy object. – Puritanical