验收测试打破了我的模型单元测试套件,ember-cli 0.44

时间:2014-09-15 21:46:37

标签: ember.js ember-data ember-cli

这里是如何重现的。使用ember-cli 0.44,创建一个新应用,并生成useraddress模型。他们彼此belongsTo

然后,为address编写一个单元测试,如下所示:

test('it has a user', function() {
  var model = this.subject();
  var store = this.store();

  Ember.run(function() {
    var user = store.createRecord('user');
    model.set('user', user);

    ok(true);
  });
});

needs: ['model:user']添加到测试中。测试通过了。

现在,输入ember g acceptance-test index。单位测试打破了模型,就在model.set行:

Died on test #1     at test (http://localhost:4200/assets/test-support.js:278:13)
    at eval (ember-wtf/tests/unit/models/address-test.js:21:5)
    at requireModule (http://localhost:4200/assets/vendor.js:70:29)
    at http://localhost:4200/assets/test-loader.js:14:29: Assertion Failed: You can only add a 'user' record to this relationship
Source:     
Error: Assertion Failed: You can only add a 'user' record to this relationship
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:26705:23)
    at Object.Ember.assert (http://localhost:4200/assets/vendor.js:16889:15)
    at null.<anonymous> (http://localhost:4200/assets/vendor.js:70696:17)
    at Descriptor.ComputedPropertyPrototype.set (http://localhost:4200/assets/vendor.js:25311:22)
    at set (http://localhost:4200/assets/vendor.js:30004:14)
    at __exports__.default.Mixin.create.set (http://localhost:4200/assets/vendor.js:44299:9)
    at eval (ember-wtf/tests/unit/models/address-test.js:29:15)
    at Object.Backburner.run (http://localhost:4200/assets/vendor.js:13365:27)
    at apply (http://localhost:4200/assets/vendor.js:31547:27)

有什么想法吗?单元测试中不允许store.createRecord吗?

参考文献:

2 个答案:

答案 0 :(得分:3)

事实证明商店引用不同,即使this.store()看起来像是检索商店的有福方式,但它与用于制作model的方式不同:

var model = this.subject();

model.store.toString();  //<DS.Store:ember816>
this.store().toString(); //<DS.Store:ember817>

所以,当我尝试使用model.set('relationship'..时,Ember认为它不是来自同一个商店,并且断言失败了。

使用model.store似乎可以解决问题。


我认为这是this.store测试助手中的错误。

答案 1 :(得分:0)

对于最新版本的ember-cli,Ember和Ember数据,这个问题没有发生。 Github问题也很接近https://github.com/ember-cli/ember-cli-qunit/issues/42