【发布时间】:2017-11-17 11:38:21
【问题描述】:
我正在尝试在新应用上设置 mirage 以进行测试。
ember-cli: 2.16.2
ember-cli-海市蜃楼:0.4.0
我有一个虚拟测试,只是试图设置 mirage 并验证它是否正常工作。
我会做类似于测试route.model() 的事情。
使用 mirage 的JSONAPISerializer,我的工厂和 migrage-model 什么都没有。
// models/trip.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
});
我的测试:
import {moduleFor, test} from 'ember-qunit';
import {startMirage} from 'frontend/initializers/ember-cli-mirage';
moduleFor('route:trips.index', 'Unit | Route | trips.index', {
needs: ['service:session', 'model:trip', 'adapter:application'],
beforeEach() {
this.server = startMirage();
},
afterEach() {
this.server.shutdown();
}
});
test('model', function (assert) {
let route = this.subject();
this.server.create('trip');
Ember.run(() => {
this.get('store').findAll('trip')
});
assert.ok(route);
});
我收到此错误:
TypeError: Cannot read property 'push' of null
at Class._setupRelationshipsForModel (http://localhost:4200/assets/vendor.js:196482:36)
at Class._pushInternalModel (http://localhost:4200/assets/vendor.js:196473:10)
at http://localhost:4200/assets/vendor.js:196425:20
at Backburner.run (http://localhost:4200/assets/vendor.js:20213:36)
at Backburner.join (http://localhost:4200/assets/vendor.js:20222:33)
at Class._push (http://localhost:4200/assets/vendor.js:196397:50)
at http://localhost:4200/assets/vendor.js:192955:18
at tryCatcher (http://localhost:4200/assets/vendor.js:63559:21)
at invokeCallback (http://localhost:4200/assets/vendor.js:63737:33)
at publish (http://localhost:4200/assets/vendor.js:63723:9)
在开发/生产和使用真实服务器获取数据方面工作良好。
如果我不使用 mirage 创建我的记录,也不例外。
看起来问题只发生在Ember.run内
删除 Ember.run 不会引发异常,但我需要它来正确测试(并避免像 You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run 这样的错误)...
【问题讨论】:
-
如果没有
Ember.run也不会引发异常,那为什么还要使用呢? -
有时我必须使用
Ember.run,否则我会收到此错误“您已打开测试模式,这禁用了运行循环的自动运行。您需要将任何具有异步副作用的代码包装在跑步”——这是有道理的。
标签: ember.js ember-data ember-cli ember-cli-mirage