Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between createspy and createspyobj

I have used in my code like.

return $provide.decorator('aservice', function($delegate) {             $delegate.addFn = jasmine.createSpy().andReturn(true);             return $delegate;         }); 

In that what createSpy do? can i change the createSpy calls to createspyobj calls.

By using createSpy, we can create one function/method mocks. Createspyobj can do multiple functions mocks. Am i right?

What would be the difference.

like image 399
user3686652 Avatar asked Jun 20 '14 06:06

user3686652


People also ask

What is createSpyObj?

The createSpyObj() creates a mock object with multiple spies. The created object has the spy methods as its properties, with their respective return values as its values.

Why do we use spyOn?

spyOn() spyOn() is inbuilt into the Jasmine library which allows you to spy on a definite piece of code.

Which method can be used to create Jasmine spy?

There are two ways to create a spy in Jasmine: spyOn() can only be used when the method already exists on the object, whereas jasmine. createSpy() will return a brand new function: //spyOn(object, methodName) where object. method() is a function spyOn(obj, 'myMethod') //jasmine.

How do I spy on object property in Jasmine?

In Jasmine, you can do anything with a property spy that you can do with a function spy, but you may need to use different syntax. Use spyOnProperty to create either a getter or setter spy. it("allows you to create spies for either type", function() { spyOnProperty(someObject, "myValue", "get").


1 Answers

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.

If you want to create a mock you should use jasmine.createSpyObj. Check out the examples below.

From the Jasmine documentation http://jasmine.github.io/2.0/introduction.html...

createSpy:

describe("A spy, when created manually", function() {   var whatAmI;    beforeEach(function() {     whatAmI = jasmine.createSpy('whatAmI');      whatAmI("I", "am", "a", "spy");   });    it("is named, which helps in error reporting", function() {     expect(whatAmI.and.identity()).toEqual('whatAmI');   });    it("tracks that the spy was called", function() {     expect(whatAmI).toHaveBeenCalled();   });    it("tracks its number of calls", function() {     expect(whatAmI.calls.count()).toEqual(1);   });    it("tracks all the arguments of its calls", function() {     expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");   });    it("allows access to the most recent call", function() {     expect(whatAmI.calls.mostRecent().args[0]).toEqual("I");   }); }); 

createSpyObj:

describe("Multiple spies, when created manually", function() {   var tape;    beforeEach(function() {     tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);      tape.play();     tape.pause();     tape.rewind(0);   });    it("creates spies for each requested function", function() {     expect(tape.play).toBeDefined();     expect(tape.pause).toBeDefined();     expect(tape.stop).toBeDefined();     expect(tape.rewind).toBeDefined();   });    it("tracks that the spies were called", function() {     expect(tape.play).toHaveBeenCalled();     expect(tape.pause).toHaveBeenCalled();     expect(tape.rewind).toHaveBeenCalled();     expect(tape.stop).not.toHaveBeenCalled();   });    it("tracks all the arguments of its calls", function() {     expect(tape.rewind).toHaveBeenCalledWith(0);   }); }); 
like image 185
j_buckley Avatar answered Oct 19 '22 02:10

j_buckley