我最近开始从茉莉花1.3迁移到2.0,遇到了一些问题。
这是我的旧测试的样子:
it("should start heartbeat after successful login and stop heartbeat after logout", function () {
runs(function () {
auth.hbTimeout = 500;
var loggedIn = auth.login("USERWITHSESSION", "xyz", {});
expect(loggedIn).toBe(true);
expect(auth.getAuthenticated()).toBe(true);
expect(auth.user).toBeDefined();
expect(auth.user.Session).toEqual(74790750);
setTimeout(function () {
auth.stopHeartbeat();
auth.user.Session = 74790760;
}, 2000);
});
waitsFor(function () {
return auth.user.Session == 74790760;
}, "The session-id should have been changed", 2600);
runs(function () {
auth.heartbeat();
expect(auth.getAuthenticated()).toBe(false);
expect(auth.user).not.toBeDefined();
auth.login("USERWITHSESSION", "xyz", {});
setTimeout(function () {
auth.user.Session = 74790750;
}, 500);
});
waitsFor(function () {
return auth.user.Session == 74790750;
}, "The session-id should have been changed back", 1100);
runs(function () {
setTimeout(function () {
auth.logout();
}, 2000);
});
waitsFor(function () {
return auth.getAuthenticated() == false;
});
expect(auth.user).not.toBeDefined();
});
我想复制部分直到第一个waitsFor()。对于两秒钟的超时,我尝试了setTimout()并将期望值移入afterEach。
据我了解,茉莉花应该等待两秒钟然后执行代码,但是期望总是错误的,并且测试失败。
这是我的方法:
describe("this is a async nested describe",function(){
afterEach(function(done){
expect(auth.user.Session).toBe(74790760);
});
it("let's do this",function(){
auth.hbTimeout = 500;
var loggedIn = auth.login("USERWITHSESSION", "xyz", {});
expect(loggedIn).toBe(true);
expect(auth.getAuthenticated()).toBe(true);
expect(auth.user).toBeDefined();
expect(auth.user.Session).toEqual(74790750);
setTimeout(function() {
auth.stopHeartbeat();
auth.user.Session = 74790760;
done();
},2000);
});
});
我敢肯定我做错了什么。有人可以给我提示吗?哦,无论我做什么,即使我将超时设置为一分钟,测试仍会在相同的时间内达到预期的效果。
提前致谢!
您没有将done
函数传递到let's do this
规范中。Jasmine 2.0根据length
规范功能的属性以同步或异步方式运行规范,因此no-args函数将始终同步运行。
下面的代码来自Jasmine的GitHub(/src/core/QueueRunner.js
)。
for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) {
var fn = fns[iterativeIndex];
if (fn.length > 0) {
return attemptAsync(fn);
} else {
attemptSync(fn);
}
}
另外,不要忘记也要调用done()
afterEach函数,如下所示:
afterEach(function(done){
expect(auth.user.Session).toBe(74790760);
done();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句