我最近开始从jasmine 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()并将expect移到了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);
});
});
我确信我做的事情是愚蠢的。有人能给我一个暗示吗?哦,无论我做什么,即使我将超时设置为一分钟,测试仍然会在相同的时间内达到预期。
提前致谢!
答案 0 :(得分:3)
您未将done
功能传递到let's do this
规范中。 Jasmine 2.0基于spec函数的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);
}
}
另外,不要忘记在afterEach函数中调用done()
,如下所示:
afterEach(function(done){
expect(auth.user.Session).toBe(74790760);
done();
});