I'm working on an Angular service along with its Jasmine test. The test is calling f1() and spying on f2(). The function f2 takes a variable v2 and updates it by setting field 'a' to 3. I expected the function f2 to be called with v2 (as defined in f1) but my test fails on toHaveBeenCalledWith and shows that the actual call was made with the object as it appears after the f2 function call. Is Jasmine checking the parameters for .toHaveBeenCalledWith after the function call, which might not be the recommended approach, or am I missing something 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})]
Also, note that I debugged in Chrome while testing and confirmed that the function f2() is indeed being called with v2 = {a:30}.