tha*_*adk 21 javascript jasmine
最近发布的Jasmine 2.0删除了等待函数和runs()Async Jasmine 1.3.
我有旧的1.3测试,我想转换到新的风格.
对于等待,在大多数情况下,似乎你可以写beforeEach()和afterEach()仔细地获得相同的效果.
重现runs()简单执行包含的函数的最佳方法是什么?
我的第一次尝试:
runs(function() {
expect(true).toBe(true);
}
Run Code Online (Sandbox Code Playgroud)
变
(function() {
expect(true).toBe(true);
})()
Run Code Online (Sandbox Code Playgroud)
Ton*_*ony 30
可以在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);
});
Run Code Online (Sandbox Code Playgroud)
但是,我发现这大大减慢了我的测试套件,所以我创建了自己的扩展,重新创建了waitsFor提供的轮询功能.
https://gist.github.com/abreckner/110e28897d42126a3bb9
Gre*_*egg 22
在jasmine 1.3和之前的版本中,如果你有一些异步代码,你需要等到它在完成下一部分测试之前完成之前,才需要runs和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
});
});
Run Code Online (Sandbox Code Playgroud)
茉莉花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
});
Run Code Online (Sandbox Code Playgroud)