Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing in Ember.js

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.

like image 955
josemota Avatar asked Sep 05 '13 10:09

josemota


2 Answers

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.

like image 115
Cyril Fluck Avatar answered Sep 26 '22 07:09

Cyril Fluck


According to @sly7_7's commentary, looking for the store inside the app via App.__container__.lookup('store:main') works.

like image 32
josemota Avatar answered Sep 24 '22 07:09

josemota