Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset call on Jasmine spy does not return

Tags:

I'm using a Jasmine (2.2.0) spy to see if a certain callback is called.

Test code:

it('tests', function(done) {   var spy = jasmine.createSpy('mySpy');   objectUnderTest.someFunction(spy).then(function() {     expect(spy).toHaveBeenCalled();     done();   }); }); 

This works as expected. But now, I'm adding a second level:

it('tests deeper', function(done) {   var spy = jasmine.createSpy('mySpy');   objectUnderTest.someFunction(spy).then(function() {     expect(spy).toHaveBeenCalled();     spy.reset();     return objectUnderTest.someFunction(spy);   }).then(function() {     expect(spy.toHaveBeenCalled());     expect(spy.callCount).toBe(1);     done();   }); }); 

This test never returns, because apparently the done callback is never called. If I remove the line spy.reset(), the test does finish, but obviously fails on the last expectation. However, the callCount field seems to be undefined, rather than 2.

like image 459
Jorn Avatar asked Jul 21 '15 13:07

Jorn


People also ask

How do you get rid of jasmine spy?

if are already spying on a method and you want the original method to be called instead you should call andCallThrough() which will override the first spy behavior.

How do I spy on service 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").

What is Spy object in Jasmine?

Jasmine spy is another functionality which does the exact same as its name specifies. It will allow you to spy on your application function calls. There are two types of spying technology available in Jasmine.


1 Answers

The syntax for Jasmine 2 is different than 1.3 with respect to spy functions. See Jasmine docs here.

Specifically you reset a spy with spy.calls.reset();

This is how the test should look like:

// Source var objectUnderTest = {     someFunction: function (cb) {         var promise = new Promise(function (resolve, reject) {             if (true) {                 cb();                 resolve();             } else {                 reject(new Error("something bad happened"));             }         });         return promise;     } }  // Test describe('foo', function () {     it('tests', function (done) {         var spy = jasmine.createSpy('mySpy');         objectUnderTest.someFunction(spy).then(function () {             expect(spy).toHaveBeenCalled();             done();         });     });     it('tests deeper', function (done) {         var spy = jasmine.createSpy('mySpy');         objectUnderTest.someFunction(spy).then(function () {             expect(spy).toHaveBeenCalled();             spy.calls.reset();             return objectUnderTest.someFunction(spy);         }).then(function () {             expect(spy).toHaveBeenCalled();             expect(spy.calls.count()).toBe(1);             done();         });     }); }); 

See fiddle here

like image 186
Eitan Peer Avatar answered Sep 19 '22 13:09

Eitan Peer