Reputation: 315
I have learned recently that spyOn cannot be used with external dependencies and can only be used with System Under Test.
But I have some questions regarding why it cant be used and I came up with very few answers.
So I would like to know the best practices where we should use each of them.
Upvotes: 1
Views: 2700
Reputation: 1315
We can use jasmine.createSpy when we need to spy a function e.g.
let router = { navigate: jasmine.createSpy("navigate") },
here we're creating spy function using jasmine.createSpy
jasmine.createSpyObj is used to create spy class/obj which has methods e.g.
let service = jasmine.createSpyObj("ApiService", ["getData"])}
here ApiService is class which has getData function.
Upvotes: 2
Reputation: 2363
jasmine.createSpy can be used when there is no function to spy on. It will track calls and arguments like a spyOn but there is no implementation.
jasmine.createSpyObj is used to create a mock that will spy on one or more methods. It returns an object that has a property for each string that is a spy.
you should have a method on the object with spyOn.The advantage of the spyOn is that you can call the original method
Upvotes: 5