ember-qunit:你只能卸载不在飞行中的记录

时间:2015-03-06 19:11:20

标签: ember.js qunit ember-qunit

我有一些访问商店的单元测试。我会认为这样会好的,只要我将它们包装在运行回调中。不幸的是,事实并非如此。我收到了这个错误:

afterEach在#foo上失败:断言失败:您只能卸载不在飞行中的记录。

据我所知,这正是run应该阻止的。我的测试看起来像这样:

test('#foo', function(assert) {
  var store = this.store();
  var model = this.subject();

  Ember.run(function() {
    var secondModel = store.createRecord('secondModel', { foo: 'bar' });
    model.set('secondModel', secondModel);
    var foo = model.get('secondModelFoo');

    assert.equal(foo, 'bar');
  });
});

1 个答案:

答案 0 :(得分:3)

在Ember Data v1.13.8中与Ember v1.13.7结合使用似乎不再是一个问题。

以下设置:

模型/先model.js

import DS from 'ember-data';

export default DS.Model.extend({
  secondModel: DS.belongsTo('second-model')
});

模型/第二model.js

import DS from 'ember-data';

export default DS.Model.extend({
  foo: DS.attr('string')
});

的测试/单元/模型/第一模型test.js

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

moduleForModel('first-model', 'Unit | Model | first model', {
  // Specify the other units that are required for this test.
  needs: ['model:second-model']
});

test('it exists', function(assert) {
  var model = this.subject();
  // var store = this.store();
  assert.ok(!!model);
});

test('#foo', function(assert) {
  var store = this.store();
  var model = this.subject();

  Ember.run(function() {
    assert.expect(1);

    var secondModel = store.createRecord('second-model', { foo: 'bar' });
    model.set('secondModel', secondModel);
    var foo = model.get('secondModel.foo');

    assert.equal(foo, 'bar');
  });
});

测试通过。 Demo project repository on GitHub.