Given Ember reached 1.0.0
recently, I wanted to start using it with tests. I'm using Yeoman 1.0 with Karma. I want to unit test models but I'm finding it very difficult to accomplish isolation.
The example I have now is:
describe("Expense", function() {
return it("has a computed property called `explained`", function() {
var expense = App.Expense.create({
name: "My first expense",
value: 34
});
return expect(expense.get("explained")).to.equal("My first expense -- 34");
});
});
As of 1.0.0
, I get the following error:
Error: You should not call `create` on a model. Instead, call
`store.createRecord` with the attributes you would like to set.
How should I access store in order to create a model instance? More ideally, how can I simply spawn models like this without even resorting to the store, is that viable? There's no point in spawning an entire app just to test a model, IMO.
Thank you.
Here is the minimum code that I've used so far for unit testing models.
var container, store, expense;
container = new Ember.Container();
container.register('store:main', DS.Store.extend());
container.register('model:expense', App.Expense);
store = container.lookup('store:main');
Ember.run( function() {
expense = store.createRecord('expense', {
name: "My first expense",
value: 34
});
});
Based on the code of the store and the way models are tested inside Ember Data, I don't think that you can reduce the setup of the test.
According to @sly7_7's commentary, looking for the store inside the app via App.__container__.lookup('store:main')
works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With