首页 > 解决方案 > Ember cli mirage:在验收测试中使用夹具

问题描述

我正在尝试在我的验收测试中使用固定装置,但是由于重复数据,它会导致测试失败,因为每次测试都会将固定装置重新插入海市蜃楼数据库中。有没有办法解决这个问题或在每次测试时移除固定装置

setupApplicationTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function() {
    this.server.loadFixtures();
});

标签: ember.jsember-cli-mirage

解决方案


您在上面向我展示的这段代码来自哪里?

在验收测试中,Mirage 自动从它捆绑在其插件中的初始化程序启动/停止服务器addon/instance-initializers/ember-cli-mirage-autostart.js

let server = startMirage(appInstance);
testContext.server = server;

// To ensure that the server is shut down when the application is
// destroyed, register and create a singleton object that shuts the server
// down in its willDestroy() hook.
appInstance.register('mirage:shutdown', MirageShutdown);

调用:

willDestroy() {
   let testContext = this.get('testContext');
   testContext.server.shutdown();
   delete testContext.server;
}

Ember 在每次测试之间启动和停止应用程序,这意味着每次验收测试都会自动从空数据库开始。

如果您不在验收测试的范围内,则需要先停止自己。

// tests/integration/components/your-test.js
import { startMirage } from 'yourapp/initializers/ember-cli-mirage';

moduleForComponent('your-component', 'Integration | Component | your component', {
  integration: true,
  beforeEach() {
    this.server = startMirage();
  },
  afterEach() {
    this.server.shutdown();
  }
});

每次测试后调用关机对于清除数据至关重要


推荐阅读