在使用ember mocha中的所有钩子之前使用TypeError

时间:2019-07-31 21:24:39

标签: ember.js mocha

我在ember-mocha(版本0.14.0)中遇到了“所有挂钩”之前的问题。这是一个来自the docs的示例,该示例经过了稍微修改,以包含一个beforeEach钩子:

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { visit, currentURL } from '@ember/test-helpers';

describe('basic acceptance test', function() {
  setupApplicationTest();

  beforeEach(async function() {
    await visit('index');
  });

  it('can visit /', async function() {
    await visit('/');
    expect(currentURL()).to.equal('/');
  });
});

以上测试按预期运行,没有问题。但是,当我用before代替beforeEach时遇到错误:

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { visit, currentURL } from '@ember/test-helpers';

describe('basic acceptance test', function() {
  setupApplicationTest();

  before(async function() {
    await visit('index');
  });

  it('can visit /', async function() {
    await visit('/');
    expect(currentURL()).to.equal('/');
  });
});
TypeError: Cannot destructure property `owner` of 'undefined' or 'null'.
    at visit (assets/test-support.js:24931:9)
    at Context.<anonymous> (assets/tests.js:339:36)
    at invoke (assets/test-support.js:22801:21)
    at Context.asyncFn (assets/test-support.js:22786:11)
    at callFnAsync (assets/test-support.js:14070:8)
    at Hook.Runnable.run (assets/test-support.js:14022:7)
    at next (assets/test-support.js:14386:10)
    at assets/test-support.js:14408:5
    at timeslice (assets/test-support.js:9651:27)

请让我知道是否需要任何澄清。预先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

那是意料之中的! before对于所有测试仅运行一次。 这意味着在所有beforeEach钩子之前 但是setupApplicationTest使用beforeEach来设置应用程序(和容器),并使用afterEach再次将其拆解。 这意味着您将获得适用于所有测试的全新应用程序。 但是,没有应用程序,您将无法真正访问任何东西。

这意味着对于每次测试,您都会获得一个新的应用程序实例。 这意味着没有用于所有测试的应用程序,因此没有可以访问路线的应用程序。


在余烬纷争频道中也问了同样的问题。这个答案试图利用讨论的实质将其归档在SO上。