Ember cli mirage:在验收测试中使用夹具



我正试图在验收测试中使用fixture,但由于每次测试都会将fixture重新插入到mirage DB中,这会导致测试由于重复数据而失败。有没有办法绕过这个问题,或者在每次测试时移除固定装置

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

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

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

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

最新更新