【问题标题】:Jasmine 2.0 async beforeEach not waiting for async to finishJasmine 2.0 async beforeEach 不等待异步完成
【发布时间】:2014-10-25 05:46:48
【问题描述】:

我正在使用 Jasmine 2.0 和 require.js。当我将异步代码放在 beforeEach 函数中时,我无法让异步测试正常工作。在异步调用完成之前,我的 it 语句仍在运行。

这是我的规格:

describe("App Model :: ", function() {
    var AppModel;

    beforeEach(function(done) {
        require(['models/appModel'], function(AppModel) {
            AppModel = AppModel;
            done();
        });
    });

    // THIS SPEC FAILS AND RUNS BEFORE ASYNC CALL
    it("should exist", function(done) {
        this.appModel = new AppModel()
        expect(this.appModel).toBeDefined();
        done();
    });

    // THIS SPEC PASSES
    it("should still exist", function(done) {
        require(['models/appModel'], function(AppModel) {
            this.appModel2 = new AppModel()
            expect(this.appModel2).toBeDefined();
            done();
        });
    });

});

当我在it 中包含异步时,第一个规范失败但第二个规范通过。

理想情况下,我希望 beforeEach async 能够正常工作,而不是不干燥并将每个 require 复制到单独的 it 语句中。

有什么建议吗?

【问题讨论】:

    标签: javascript asynchronous jasmine require


    【解决方案1】:

    Local require var 应该有另一个名称来包装到外部范围。同样在您不需要完成的“它”中,它仅在异步部分中。这样的事情必须有效:

    describe("App Model :: ", function() {
      var AppModel;
    
      beforeEach(function(done) {
        require(['models/appModel'], function(_AppModel) {
            AppModel = _AppModel;
            done();
        });
      });
    
      it("should exist", function() {
        var appModel = new AppModel()
        expect(appModel).toBeDefined();
      });
    
    });
    

    【讨论】:

    • 啊,是的!谢谢你。我想我不需要“它”中的异步,但正在尝试一切。
    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2021-06-27
    • 2019-06-16
    • 2018-03-05
    • 2018-12-20
    • 2019-05-31
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多