javascript Jasmine 2.0:重构 1.3 的 running() 和 waitsFor()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20963038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine 2.0: refactoring out 1.3's runs() and waitsFor()
提问by thadk
The recently released Jasmine 2.0 removes the waits functionsand the runs()
from the Async Jasmine 1.3.
最近发布的 Jasmine 2.0从 Async Jasmine 1.3 中删除了等待功能和runs()
。
I have old 1.3 tests I'd like to transition to the new style.
我有旧的 1.3 测试,我想转换到新样式。
For the waits, in most cases it seems like you can write beforeEach()
and afterEach()
carefully for the same effect.
对于等待,在大多数情况下,您似乎可以仔细编写beforeEach()
并afterEach()
获得相同的效果。
What is the best way to reproduce the runs()
which simply executes the contained functions sequentially?
重现runs()
简单地按顺序执行包含的函数的最佳方法是什么?
My first try:
我的第一次尝试:
runs(function() {
expect(true).toBe(true);
}
becomes
变成
(function() {
expect(true).toBe(true);
})()
回答by Tony
It is possible to use a setTimeout in your it() block.
可以在 it() 块中使用 setTimeout。
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);
});
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.
但是,我发现这显着降低了我的测试套件的速度,因此我创建了自己的扩展,它重新创建了 waitsFor 提供的轮询功能。
回答by Gregg
In jasmine 1.3 and previous, the runs
and waits
/waitsFor
should have only been necessary if you had some asynchronous code that you needed to wait until it was done before doing the next part of the test. In that case you would have something like:
在 jasmine 1.3 及更早版本中,只有当您有一些异步代码需要等待它完成后再进行下一部分测试时,才需要runs
and waits
/ waitsFor
。在这种情况下,你会有类似的东西:
it("is asynchronous", function() {
var isItDone = false;
runs(function() {
$.ajax('/some/url').success(function() { isItDone = true; });
});
waitsFor(function() {
return isItDone;
});
runs(function() {
// this won't run until the waitsFor returns true
});
});
Jasmine 2.0 moved to using a done
callback for beforeEach
, it
, and afterEach
if they do something asynchronous that you need to wait for.
茉莉花2.0移到使用done
回调beforeEach
,it
以及afterEach
如果他们做一些事情异步,你需要等待。
beforeEach(function(done) {
$.ajax('/some/url').success(done);
});
it("is asynchronous", function() {
// this won't run until the done callback is invoked from the beforeEach
});