如何在 this.owner` 中作为 属性 在 Ember js 测试中访问?

How to access in `this.owner` as a property in Ember js test?

我添加了一个带有一些额外逻辑的 ember 适配器,然后我想在 ember 测试中测试该逻辑。

在测试文件中,我看到此行在每个测试中重复多次 let adapter = this.owner.lookup('adapter:assessment');

我想将该行移动到 class 级别属性中以访问我的适配器。 我怎么做? 我认为这与 this.owner.lookup is only accessible within test() 有关。

(使用 ember js 3.2.6)

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Adapter | assessment', function (hooks) {
  setupTest(hooks);
  let myVar = 'abc';
  let adapterVar = this.owner.lookup('adapter:assessment'); // This fails! How to do this correctly?

  test('it exists', function (assert) {
    let adapter = this.owner.lookup('adapter:assessment');
    console.log(myVar); // This works!
    assert.ok(adapter);
  });

  test('Test 1', function (assert) {
    let adapter = this.owner.lookup('adapter:assessment');
    console.log(myVar);      // This works!
    console.log(adapterVar); // This fails!
    assert.ok(adapter);
  });

  test('Test 2', function (assert) {
    let adapter = this.owner.lookup('adapter:assessment');
    console.log(myVar);      // This works!
    console.log(adapterVar); // This fails!
    assert.ok(adapter);
  });

  test('Test 3', function (assert) {
    let adapter = this.owner.lookup('adapter:assessment');
    console.log(myVar);      // This works!
    console.log(adapterVar); // This fails!
    assert.ok(adapter);
  });

});


怎么样:

module('Unit | Adapter | assessment', function (hooks) {
  setupTest(hooks);

  let adapterVar;

  hooks.beforeEach(function () {
    adapterVar = this.owner.lookup('adapter:assessment')
  });
 
  test('Test 1', function (assert) {
    console.log(adapterVar);
    assert.ok(adapterVar);
  });
});