gpt4 book ai didi

jasmine - 从 Jasmine 1.3 迁移到 2.0 的问题 done 和 SetTimeOut

转载 作者:行者123 更新时间:2023-12-04 04:39:28 25 4
gpt4 key购买 nike

我最近开始从 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() 并将期望移到了 afterEach 中。

据我了解, Jasmine 应该等待两秒钟然后执行代码,但期望总是错误并且测试失败。

我是这样做的:

 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 属性以同步或异步方式运行规范,因此无参数函数将始终同步运行。

以下代码来自 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();
});

关于jasmine - 从 Jasmine 1.3 迁移到 2.0 的问题 done 和 SetTimeOut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com