問題描述
最近發布的 Jasmine 2.0 刪除了 等待函數以及來自 Async Jasmine 1.3 的 runs()
.
The recently released Jasmine 2.0 removes the waits functions and the runs()
from the Async Jasmine 1.3.
我有舊的 1.3 測試想要轉換到新樣式.
I have old 1.3 tests I'd like to transition to the new style.
對于等待,在大多數情況下,您似乎可以仔細編寫 beforeEach()
和 afterEach()
以獲得相同的效果.
For the waits, in most cases it seems like you can write beforeEach()
and afterEach()
carefully for the same effect.
重現 runs()
的最佳方法是簡單地按順序執行包含的函數?
What is the best way to reproduce the runs()
which simply executes the contained functions sequentially?
我的第一次嘗試:
runs(function() {
expect(true).toBe(true);
}
變成
(function() {
expect(true).toBe(true);
})()
推薦答案
可以在 it() 塊中使用 setTimeout.
It is possible to use a setTimeout in your it() block.
it("is asynchronous", function(done) {
var isItDone = false;
$.ajax('/some/url').success(function() { isItDone = true; });
setTimeout(function(){
expect(isItDone).toBeTrue();
done(); // call this to finish off the it block
}, 500);
});
但是,我發現這大大降低了我的測試套件的速度,因此我創建了自己的擴展程序,該擴展程序重新創建了 waitsFor 提供的輪詢功能.
However, I found that that slowed down my test suite dramatically so I created my own extension which recreates the polling functionality that waitsFor provided.
https://gist.github.com/abreckner/110e28897d42126a3bb9
這篇關于Jasmine 2.0:重構 1.3 的 runs() 和 waitsFor()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!