Lorraine Bernard
Lorraine Bernard

Reputation: 13400

How should I test/spy this kind of event in backbone.marioentte

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 Expected spy restartPolling to have been called.
The module works but the test fails. any ideas?


P.S.:

This question is related to this one Expected a spy, but got Function


(1)

// jasmine test module

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();
        // Expected spy restartPolling to have been called.
    });
});

(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
});

(3)

// polling module

var poller = {
    restartPolling: function () {
        this.stopPolling();
        this.startPolling(options);
    },
    // other code
};

(4)

var UsersBoardCollection = Backbone.Collection.extend({
    // some code
});

_.extend(UsersBoardCollection.prototype, poller);

return UsersBoardCollection;

Upvotes: 1

Views: 648

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 110922

I'm using Marionette and I test the it a bit different. My idea is not to test, that when the event is fired on a real app that the function is called, cause this will be tested by Marionette developers.

I test that the event was binded to app.vent. Therefor I've create a spy on app.vent.on and fire the the function afterwards by myself:

spyOn(app.vent, 'on');
expect(app.vent.on.argsForCall[0][0]).toBe('onGivePoints')

app.vent.trigger.argsForCall[0][1]()

Upvotes: 1

Related Questions