Ember 测试中未定义 Mirage 服务器

Ember mirage server is not defined in tests

我正在尝试将 ember-cli-mirage 装置集成到一些测试中。我遵循了此处的文档:ember fixtures

问题:未定义服务器。错误信息: ReferenceError: server is not defined

型号-test.js:

import { moduleForModel, test } from 'ember-qunit';

moduleForModel('network', 'Unit | Model | network', {
  needs: []
});

test('it exists', function(assert) {
  server.loadFixtures('networks'); //no defined

andThen(function() {
   let net1 = networks.first();
});
  assert.ok(true);
});

我还验证了配置设置为 true。

ENV['ember-cli-mirage'] = {
        enabled: true
    }

Mirage 在初始化程序中启动。由于初始化程序仅在 Ember 完整的 Ember 应用程序启动时才可用,因此默认情况下 Mirage 服务器仅在验收测试中可用。

要在集成或单元测试中使用 Mirage,请遵循 the docs on manually starting your Mirage server。目前文档是这样说的:

To run your Mirage server during a unit or integration test, first create a helper:

// tests/helpers/start-mirage.js
import mirageInitializer from '../../initializers/ember-cli-mirage';

export default function startMirage(container) {
   mirageInitializer.initialize(container);
 }

Then, add the following to any test where you want Mirage to initialize:

// tests/integration/components/your-test.js
 import startMirage from '../../../helpers/start-mirage';

moduleForComponent('your-component', 'Integration | Component | your component', {
   integration: true,
   setup: function() {
     startMirage(this.container);
   }
 });