[英]Multiple async test jasmine
从上周开始,我遇到了茉莉花测试的问题。 我从哪里发现了该错误,但我不知道如何解决它。
这里的代码:
it("Test the time of asynchronous call", function(done) {
var isItDone = false;
model.setSuccessFnc(function(isItDone) {
isItDone = true;
expect(isItDone).toBe(true);
done();
});
model.setId("serviceLevel");
model.getDataFromBatch("srService", "workItems", query.services, null, filter, null, true);
setTimeout(function() {
expect(isItDone).toBe(true);
done();
}, 4000);
});
it("Test retrieved data of asynchronous call", function(done) {
// var isItDone = false;
model.setSuccessFnc(function(oData, oResponse) {
var json = JSON.parse(oData.__batchResponses[0].body);
expect(json.d.results.length).toBeGreaterThan(0);
done();
});
model.setId("serviceLevel");
model.getDataFromBatch("srService", "workItems", query.services, null, filter, null, true);
});
如果我评论第一个测试,那么我的第二个测试就可以了,否则我将出现以下错误:
预期假为真。
对我来说,第二项测试使用第一个“完成”。 我该如何重置?
PS:对不起,我的英语不是我的母语。
谢谢你的帮助。
几点注意事项:
测试中的setTimeout不是通常的情况,茉莉花具有自己的超时,并且期望超时是重复的。
如果使用真正的后端或某种模拟,则from sample显然不是。 使用$ httpBackend进行模拟响应,您的测试将更快。
第一次测试包含错误。 回调setSuccessFnc具有参数isItDone-它实际上是在测试开始时“覆盖” var isItDone
定义的局部变量。
如果调用了回调,则将回调中的局部isItDone设置为true,并满足回调中的期望。
但是超时声明中的第二个断言与var isItDone
一起使用仍然是错误的。
结果取决于较早发生的情况。
这应该有帮助
model.setSuccessFnc(function() {
isItDone = true;
expect(isItDone).toBe(true);
done();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.