Nikos
Nikos

Reputation: 7552

What is the correct way to inject Emberdata in an Ember integration test

I have a component that expects a model property that is an ember data object (originally created from the route model)

I've tried this in the integration test but the store is undefined

test('it renders', function (assert) {
  this.inject.service('store')

  let model = this.get('store').createRecord('post');

  this.set('model', model);

  this.render(hbs`{{post-item-form model = model}}`);

  assert.equal(this.$().text().trim(), 'Post your thoughts');

  // Template block usage:
  this.render(hbs`
    {{#post-item-form}}
      template block text
    {{/post-item-form}}
  `);

  // assert.equal(this.$().text().trim(), 'template block text');
});

Upvotes: 0

Views: 162

Answers (1)

feanor07
feanor07

Reputation: 3368

I would prefer to create a pure json object instead of creating a record through the usage of store in a component integration testing; because the component itself knows nothing about the store and you can just pass pure json object instead of a model instance to the component and it should still work. With this mindset, I would only deal with store in acceptance tests.

If you still would like to go the way you have mentioned; I believe you need to retrieve the store as follows:

var store = Ember.getOwner(this).lookup("service:store");

Since; auto run loops are disabled in testing mode by default; it is most likely that you will get an assertion error indicating that there is no run loop available when you run the following code let model = store.createRecord('post'); this means you need to wrap it in a within a run loop like Ember.run(()=>model = store.createRecord('post'));. I did not give a try to what I wrote; but I guess this should work.

Yet again; why do you need to create the record through store in an integration test? If you really like to use store; then an acceptance test should be better; since store will be up and running and you will not need to retrieve it through lookup. I hope this helps.

Upvotes: 1

Related Questions