Why use spyOn instead of jasmine.createSpy?

The difference is that you should have a method on the object with spyOn

const o = { some(): { console.log('spied') } };
spyOn(o, 'some');

while the mock method is created for your with createSpy():

const o = {};
o.some = jasmine.createSpy('some');

The advantage of the spyOn is that you can call the original method:

spyOn(o, 'some').and.callThrough();
o.some(); // logs 'spied'

And as @estus says the original method is restored after the test in case of spyOn. This should be done manually when it’s reassigned with.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)