茉莉花2.0异步beforeEach不等待异步完成 [英] Jasmine 2.0 async beforeEach not waiting for async to finish

查看:867
本文介绍了茉莉花2.0异步beforeEach不等待异步完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用茉莉花2.0和require.js。我不能当我把异步code在beforeEach功能异步测试正常工作。我这语句是异步调用完成之前仍在运行。

I am using Jasmine 2.0 and require.js. I cannot get the async tests to work properly when I put the async code in a beforeEach function. My it statement is still running before the async call finishes.

下面是我的规格:

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

});

第一规格失败,但第二个规格的推移,当我包括在异步。

在理想情况下,我想在 beforeEach 异步工作而不是不干燥和复制均需要到各个它的声明。

Ideally, I'd like the beforeEach async to work rather than being not DRY and copying each require into the individual it statements.

任何提示?

推荐答案

本地需要VAR应该有另外一个名字被包装的范围之外。此外,在它你不需要做的,它仅在异步部分。像这样的东西必须工作:

Local require var should have another name to be wrapped to the outside scope. Also in the "it" you do not require done, it is only in the async part. Something like this must work:

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

});

这篇关于茉莉花2.0异步beforeEach不等待异步完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆