【问题标题】:Jasmine 2.0 done and afterEachJasmine 2.0 完成和 afterEach
【发布时间】:2016-04-01 10:39:42
【问题描述】:

我正在使用jasmine 2.0

我正在尝试理解done() 函数。

使用以下基本的茉莉花代码:

describe('Jasmine 2.0 done()', function() {

    beforeEach(function () {
        console.log('install');
        jasmine.clock().install();
    });

    afterEach(function () {
        console.log('uninstall');
        jasmine.clock().uninstall();
    });


    it('should wait 1ms then complete', function (done) {

        setTimeout(function(){
            console.log('async');
            expect(true).toBe(true);
            done();      
        }, 1)

    });

});

我认为我看到的会发生:

  • beforeEach 运行,安装时钟,记录“安装”
  • 测试运行,setTimeout 没有做任何事情
  • 测试等待 5 秒(jasmine 失败前的默认超时时间)
  • 测试失败,因为从未调用过 done()
  • 我知道这一点是因为我收到错误:Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  • afterEach 然后运行,卸载时钟并记录“卸载”

我会期待

  • beforeEach 运行,安装时钟并记录“安装”
  • 测试运行,等待一毫秒,运行日志,expect 和done()
  • afterEach 运行,卸载时钟并记录“卸载”
  • 测试通过,没有错误

我认为这是因为文档说

在调用 done 之前,此规范不会完成。

所以我假设afterEach 会等到done() 被调用来执行。

我也尝试在afterEach 中添加done()

afterEach(function (done) {
    console.log('uninstall');
    jasmine.clock().uninstall();
    done();
});

这没什么区别。

为什么这个测试不成功?

我对@9​​87654337@ 函数有什么不了解的地方?

plunker 显示问题

【问题讨论】:

    标签: javascript unit-testing asynchronous jasmine


    【解决方案1】:

    安装 jasmine 的模拟时钟后,setTimeout 的行为将被覆盖。当时钟通过jasmine.clock().tick 函数向前计时时,将触发对任何已注册回调的调用,这需要几毫秒 (link to jasmine docs)

    当时钟通过 jasmine.clock().tick 函数向前计时时,将触发对任何已注册回调的调用,这需要数毫秒。

    在您的情况下,测试规范如下所示:

    describe('Jasmine 2.0 done()', function() {
    
        beforeEach(function () {
            console.log('install');
            jasmine.clock().install();
        });
    
        afterEach(function () {
            console.log('uninstall');
            jasmine.clock().uninstall();
        });
    
    
        it('should wait 1ms then complete', function (done) {
    
            setTimeout(function(){
                console.log('async');
                expect(true).toBe(true);
                done();      
            }, 1);
    
            jasmine.clock().tick(1);
        });
    
    });
    

    【讨论】:

    • 啊,有趣。我一定错过了。谢谢你。时钟是否对异步操作也有其他影响?即承诺(这是原始问题的来源)
    • 基于 jasmine 文档安装模拟时钟仅影响 setIntervalsetTimeout 函数,不应影响任何其他本机对象。它不应该影响原生 Promise,但它可能会弄乱自定义 Promise 实现和 polyfill 或任何其他使用 setTimeoutsetInterval 的代码。
    • 我明白了。感谢您的回答。我将使用它来尝试使用 Promise 修复我的测试,并让您知道它是否有效。
    【解决方案2】:

    使用 Jasmine 异步测试,我们必须调用 beforeEach()afterEach() 函数中的异步代码,该函数在 describe() 函数块内的每个 it() 函数块之前运行。

    这意味着必须在上述方法中调用done()回调,否则测试将变为同步,如果超过超时间隔,它将阻塞调用线程。

    beforeEach(function (done) {
        console.log('install');
        jasmine.clock().install();
        done(); 
    });
    
    afterEach(function (done) {
        console.log('uninstall');
        jasmine.clock().uninstall();
        done(); 
    });
    

    Here 是一个有效的 JSFiddle。

    【讨论】:

    • 在您的 JSFiddle 中,我没有看到 setTimeout 的函数被调用过?我没有看到它记录“异步”
    • 我忘记在我的代码中添加jasmine.clock().tick(1);,但很高兴你设法得到了答案。
    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多