[英]Jasmine: after using callFake, how can you revert to the original function?
[英]With Jasmine, how do I test an async call using callFake() and done()?
我正在使用jasmine的新語法(因此,沒有waits
或runs
)來測試異步操作,並且遇到了麻煩。
我想做的是確保在模型觸發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.