以下是如何复制。使用ember-cli
0.44创建一个新的应用程序,并生成user
和address
模型。它们彼此CCD_ 4。
然后,为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);
});
});
将CCD_ 6添加到测试中。测试通过。
现在,键入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
?
参考文献:
- 测试关系
发现存储引用不同,尽管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
测试助手中的错误。
对于最新版本的ember-cli、ember和ember数据,此问题不会发生。Github问题也已解决https://github.com/ember-cli/ember-cli-qunit/issues/42