vendredi 1 mai 2015

With Jasmine, how do I test an async call using callFake() and done()?

I am using jasmine's new syntax (so no waits, or runs) for testing asynchronous operations, and am having trouble.

What I am trying to do is ensure that the render function is called when the model triggers a sync event. This example is trivial, but demonstrates the core problem I have. This answer comes close to my use case, but is ultimately different due to the setTimeout, which I do not have.

What I've tried is:

my function

postLoad : function() {
    this.listenTo(this.model, 'sync', this.render);
},

my test

describe('postLoad', function () {

    it('listens to model sync and calls render', function (done) {
        //arrange
        view = new App.View({ model: model });
        view.initialize();
        view.postLoad();

        spyOn(view, 'render').and.callFake(function () {
            done();
        });

        //act
        view.model.trigger('sync');

        //assert
        expect(view.render.calls.count()).toBe(1);

    });
});

The above seems reasonable sense render may take a while, but from what I understand currently with done, the spec will not evaluate until it's called. I am hoping someone can come to the rescue! Since we are faking render does it not get counted in the spy?

Aucun commentaire:

Enregistrer un commentaire