簡體   English   中英

使用Jasmine,如何使用callFake()和done()測試異步調用?

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

我正在使用jasmine的新語法(因此,沒有waitsruns來測試異步操作,並且遇到了麻煩。

我想做的是確保在模型觸發sync事件時調用render函數。 這個例子很簡單,但是說明了我遇到的核心問題。 這個答案很接近我的用例,但由於setTimeout而最終有所不同,而我沒有。

我試過的是:

我的功能

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

我的測試

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

    });
});

以上似乎合理的render可能需要一段時間,但是根據我目前對done了解,直到調用規范done ,規范才會評估。 我希望有人能救出來! 既然我們是在偽裝渲染,它不會被計入間諜中嗎?

只要this.view.model.trigger('sync'); 似乎觸發了對view.render的調用,我認為這應該可以工作。

describe('postLoad', function() {
    beforeEach(function(done) {
        this.view = new App.View({ model: model });
        spyOn(this.view, 'render').and.callFake(done);
        this.view.initialize();
        this.view.postLoad();
        this.view.model.trigger('sync');
    });
    it('listens to model sync and calls render', function() {
        expect(this.view.render.calls.count()).toBe(1);
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM