【问题标题】:Ember cli mirage: using fixtures in acceptance testEmber cli mirage:在验收测试中使用夹具
【发布时间】:2018-11-06 18:59:52
【问题描述】:

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

setupApplicationTest(hooks);
setupMirage(hooks);

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

【问题讨论】:

  • setupMirage 应该关闭 Mirage 数据库并在每次测试时重新启动它,因此您不应该有重复的数据。
  • 是的,我会,但似乎并非如此

标签: ember.js ember-cli-mirage


【解决方案1】:

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

在验收测试中,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();
  }
});

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

【讨论】:

    猜你喜欢
    • 2015-10-25
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多