Reset call on Jasmine spy does not return
Asked Answered
B

2

23

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.

Bluebird answered 21/7, 2015 at 13:0 Comment(3)
Try adding rejection handlers for your promises.Benoni
added .catch(done) to the end of the chain, same problem occurs. So @Daniel, if it's throwing I can't detect it.Bluebird
Adding a try/catch does reveal the exception: 'undefined' is not a function (evaluating 'spy.reset()'). I'm not sure why this isn't correctly handled by Q / Jasmine. I'm also not sure why my spy is missing several properties it normally should have.Bluebird
G
41

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

Gaberones answered 23/7, 2015 at 13:18 Comment(0)
P
5

Another way to write it:

const spy = spyOn(foo, 'bar');
expect(spy).toHaveBeenCalled();
spy.calls.reset();
Pappus answered 7/9, 2017 at 19:34 Comment(2)
If you are using typescript, very likely bar method does not have calls key.Colima
Good call. Fixed.Pappus

© 2022 - 2024 — McMap. All rights reserved.