41

I am trying to implement a test (1) for this module (2).
My purpose is to check if the collection is fetched when a particular event is triggered.
As you can see from my comment in (2) I get the message Error: Expected a spy, but got Function.
The module works but the test fails. any ideas?


(1)

// jasmine test module

describe('When onGivePoints is fired', function () {
    beforeEach(function () {
        spyOn(this.view.collection, 'restartPolling').andCallThrough();
        app.vent.trigger('onGivePoints');
    });
    it('the board collection should be fetched', function () {
        expect(this.view.collection.restartPolling).toHaveBeenCalled();
       // Error: Expected a spy, but got Function.
    });
});

(2)

// model view module
return Marionette.CompositeView.extend({
    initialize: function () {
        this.collection = new UserBoardCollection();
        this.collection.startPolling();
        app.vent.on('onGivePoints', this.collection.restartPolling);
    },
    // other code
});
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134
  • there's not enough code to see what's going on. please include more than just the individual functions - include the object definition that the functions belong to, and the code that instantiates the objects, at least. – Derick Bailey Aug 21 '12 at 12:52
  • @DerickBailey thanks for your time. I updated my question with mode code. – Lorraine Bernard Aug 21 '12 at 14:28
  • I use QUnit rather than Jasmine, but shouldn't your call to app.vent.trigger be in the "it" method rather than beforeEach? – codemonkey Aug 21 '12 at 14:53
  • @codemonkey, @DerickBailey, actually I got a different error: `Expected a spy, but got Function`. I updated my question. – Lorraine Bernard Aug 21 '12 at 14:59

3 Answers3

53

You need to get into the actual method, which in this case is on the prototype.

describe('When onGivePoints is fired', function () {
    beforeEach(function () {
        spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough();
        app.vent.trigger('onGivePoints');
    });
    it('the board collection should be fetched', function () {
        expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
    });
});

Spying on the prototype is a nice trick you can use when you can't get to the actual instance you want to spy on.

antonjs
  • 14,060
  • 14
  • 65
  • 91
  • if the function does not exist on the prototype, and is a function inside a React class, is this expected to work? – zero_cool Apr 19 '18 at 18:55
4

I was also getting the same issue but I resolved it by passing an argument in the function call. Then you have to write your test case like this in the it

var data = {name:"test"}
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough();
UsersBoardCollection.prototype.restartPolling(data);
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
Tabish
  • 1,592
  • 16
  • 13
0

I had this bug because I had two versions of sinon loaded, or possibly i wasn't initialising sinon-jasmine correctly. When I explicitly loaded sinon and then sinon jasmine in my spec setup, it started running correctly.

bnolan
  • 27
  • 1
  • 2