【问题标题】:Problems Migrating from Jasmine 1.3 to 2.0 Problems with done and SetTimeOut从 Jasmine 1.3 迁移到 2.0 的问题 done 和 SetTimeOut 的问题
【发布时间】:2021-05-22 17:22:56
【问题描述】:

我最近开始从 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);
    });

});

谁能给我一个提示?无论我做什么,即使我将超时设置为一分钟,测试仍然会在相同的时间内达到预期。

【问题讨论】:

    标签: jasmine


    【解决方案1】:

    您没有将 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();
    });
    

    【讨论】:

    • 您好 Eric,首先感谢您的回答。因此,通过将 done 函数传递给我的 let's do this 规范来修改它最终会出现一个非常令人困惑的错误:Error - Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 我没有使用默认超时间隔,而是设置了 2000 ms ,它应该覆盖默认超时间隔。当我删除 setTimeout 调用中的 done() 调用之外的所有代码时,甚至会发生这种情况
    • 感谢您的回答,它现在可以工作了。好像我也忘了在 afterEach 的末尾添加 done(); 。谢谢
    • 看起来你的afterEach 中没有任何东西要求它本身是异步的。通过使it 异步,您的afterEach 将不会被调用,直到您在那里调用done 回调。您应该可以从您的 afterEach 中完全删除 done 参数。
    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2017-03-29
    • 2014-03-27
    • 1970-01-01
    • 2014-05-19
    相关资源
    最近更新 更多