Reputation: 12684
Here's how to reproduce. Using ember-cli
0.44, create a new app, and generate a user
and address
model. They each belongsTo
each other.
Then, write a unit test for the address
that looks like this:
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);
});
});
adding needs: ['model:user']
to the test. The test passes.
Now, type ember g acceptance-test index
. The unit tests break for the model, right on the model.set
line:
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)
Any ideas? Is store.createRecord
not allowed in unit tests?
References:
Upvotes: 1
Views: 538
Reputation: 1209
For newest versions of ember-cli, Ember and Ember-data this problem is not happening. Github issue is close also https://github.com/ember-cli/ember-cli-qunit/issues/42
Upvotes: 0
Reputation: 12684
Turns out the store references were different, and even though this.store()
seems like the blessed way to retrieve the store, it's a different from what was used to make model
:
var model = this.subject();
model.store.toString(); //<DS.Store:ember816>
this.store().toString(); //<DS.Store:ember817>
So, when I tried to use model.set('relationship'..
, Ember didn't think it was coming from the same store, and the assertion failed.
Using model.store
seems to do the trick.
I believe this is bug in the this.store
test helper.
Upvotes: 3