2014-09-01 48 views
2

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

这里是我的规格:

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异步工作,而不是不干,并将每个要求复制到个人的声明。

任何提示?

回答

3

本地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(); 
    }); 

}); 
+0

啊,是的!谢谢。我想我不需要在“它”中的异步,但正在尝试一切。 – joehand 2014-09-01 13:28:54