Val*_*nko 18 javascript ajax bdd jasmine
我正在熟悉Jasmine(http://pivotal.github.com/jasmine/)并发现一些令人困惑的事情:
it("should be able to send a Ghost Request", function() {
var api = fm.api_wrapper;
api.sendGhostRequest(function(response) {
console.dir('server says: ', response);
});
expect(true).toEqual(false);
});
Run Code Online (Sandbox Code Playgroud)
按预期失败.
但是,在回调中移动expect调用:
it("should be able to send a Ghost Request", function() {
var api = fm.api_wrapper;
api.sendGhostRequest(function(response) {
console.dir('server says: ', response);
expect(true).toEqual(false);
});
});
Run Code Online (Sandbox Code Playgroud)
以某种方式通过:O
经过一些调试:api.sendGhostRequest()执行异步ajax请求,并且jasmine在请求完成之前冲过去.
因此问题是:
在确定测试结果之前,如何让jasmine等待ajax执行?
hal*_*s13 14
编辑Jasmine 2
异步测试已成为茉莉花2.任何测试需要处理异步代码可以用一个回调,这将显示测试的完成写简单得多.请参阅标题异步支持下的Jasmine 2文档
it('should be able to send a ghost request', (done) => {
api.sendGhostRequest((response) => {
console.log(`Server says ${response}`);
expect(true).toEqual(false);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
茉莉花1
在标题异步支持下的Jasmine站点上查看waitsFor()和runs().
使用run和waititsfor会强制Jasmine等待ajax调用完成或者超时.
代码看起来像:
it("should be able to send a Ghost Request", function() {
runs(function() {
api.sendGhostRequest(function(response) {
console.dir('server says: ', response);
flag = true;
});
}, 500);
waitsFor(function() {
return flag;
}, "Flag should be set", 750);
runs(function() {
expect(true).toEqual(false);
});
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,期望会失败.
| 归档时间: |
|
| 查看次数: |
14667 次 |
| 最近记录: |