2

In general I'm trying to test my ember models with jasmine. In Ember Data 1.0.0 we no longer have App.Model.find or App.Model.createRecord. Instead we have this.get('store').find and this.get('store').createRecord('model', {}).

How can I get a handle on the store in my tests?

injecting store

I've looked into injecting it but haven't had luck.

Encompass.inject(this, 'store', 'store');

I think this is because the object I'm injecting into this isn't the right type.

creating store

I've also tried creating a store locally:

describe("A Folder", function() {
  var store = null;
  var folder = null;
  beforeEach(function(){
    Encompass.Store = DS.Store.extend({
      adapter: Encompass.ApplicationAdapter
    });
    store = Encompass.Store.create();
  });

but using the store as in

store.find('folder');

results in TypeError: Cannot call method 'lookupFactory' of undefined

I think this is because the container doesn't exist (in time?)

2 Answers 2

4

Your store is trying to access the container but it isn't present. You will need to create manually. And for each model tested you will need to register in the store using:

container.register('model:folder', Encompass.Folder);

You updated test is the following:

describe("A Folder", function() {
  var store = null;
  var folder = null;
  beforeEach(function(){
    Encompass.Store = DS.Store.extend({
      adapter: Encompass.ApplicationAdapter
    });
    var container = new Ember.Container();
    container.register('model:folder', Encompass.Folder);
    store = Encompass.Store.create({
      container: container 
    });
  });
});
5
  • Thanks, that seems to get me further, now I get "No model was found for 'folder'" and "Cannot set property 'store' of undefined". It seems there is no factory?
    – Amir T
    Oct 25, 2013 at 14:32
  • I updated the answer. Your store is trying to acess the Folder model, I think that you have a model called Encompass.Folder right? Each model tested need to be registered using container.register('model:modelName', ModelClass). Oct 25, 2013 at 15:01
  • Excellent, so because I am manually creating the store I have to do whatever ember normally does. Could you point me to where that happens (documentation or source code)? I understand the container isn't documented for public consumption because it would lead to misuse as Ryan pointed out.
    – Amir T
    Oct 25, 2013 at 16:24
  • This is the container code, the methods have some documentation. Oct 25, 2013 at 17:39
  • Helped me a lot, thnx!
    – L-Ray
    Jan 5, 2014 at 14:27
2

If you want your application's store you can just look it up off the container. This is fine to do in your tests, but don't dare do this in production :)

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

1
  • Thanks Ryan, I think the container has not been set up (properly). I get undefined for App.__container__.lookup('store:main');
    – Amir T
    Oct 25, 2013 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.