Search code examples
ember.jsember-dataember-app-kit

How to get ember's store when testing with ember app kit?


I've create a Post model and i'm trying to write a simple test to check when i set and retrieve the title. I don't know how to get a handle on the store to create the new Post object though. Here's my test:

var App;

module('Acceptances - Index', {
  setup: function(){
    App = startApp();
  },
  teardown: function() {
    Ember.run(App, 'destroy');
  }
});

test('index renders', function(){
  expect(3);

  visit('/').then(function(){
    var title = find('h2#title');
    var list = find('ul li');

    equal(title.text(), 'Welcome to Ember.js');
    equal(list.length, 2);

    var text = list.text().replace(/\W/g, '');
    console.log(text);
    equal(text, 'RailsisunagiOmakaseO_o');
  });
});

test('can set title', function() {

  var store = App.get('store');
  var expectedTitle = 'Hello-world';
  var post = store.createRecord('post', {title: expectedTitle});
  var actualTitle = post.get('title');

    equal(actualTitle, expectedTitle, "");
});

I keep getting the following error though

Cannot call method 'createRecord' of undefined

which means i don't have a handle on the store. How can i get the store to create models in my tests?


Solution

  • Generally I'd advise against this, but tests are the exception.

    var store = App.__container__.lookup('store:main');