Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember-qunit: You can only unload a record which is not inFlight

I have some unit tests that access the store. I would have thought this would be fine, so long as I wrapped them in a run callback. Unfortunately, that's not the case. I'm getting this error:

afterEach failed on #foo: Assertion Failed: You can only unload a record which is not inFlight.

As I understand it, this is exactly what run should be preventing. My test looks something like this:

test('#foo', function(assert) {
  var store = this.store();
  var model = this.subject();

  Ember.run(function() {
    var secondModel = store.createRecord('secondModel', { foo: 'bar' });
    model.set('secondModel', secondModel);
    var foo = model.get('secondModelFoo');

    assert.equal(foo, 'bar');
  });
});
like image 221
nullnullnull Avatar asked Mar 06 '15 19:03

nullnullnull


1 Answers

Seems like this is no longer an issue in Ember Data v1.13.8 in combination with Ember v1.13.7.

For following setup:

models/first-model.js

import DS from 'ember-data';

export default DS.Model.extend({
  secondModel: DS.belongsTo('second-model')
});

models/second-model.js

import DS from 'ember-data';

export default DS.Model.extend({
  foo: DS.attr('string')
});

tests/unit/models/first-model-test.js

import Ember from 'ember';
import { moduleForModel, test } from 'ember-qunit';

moduleForModel('first-model', 'Unit | Model | first model', {
  // Specify the other units that are required for this test.
  needs: ['model:second-model']
});

test('it exists', function(assert) {
  var model = this.subject();
  // var store = this.store();
  assert.ok(!!model);
});

test('#foo', function(assert) {
  var store = this.store();
  var model = this.subject();

  Ember.run(function() {
    assert.expect(1);

    var secondModel = store.createRecord('second-model', { foo: 'bar' });
    model.set('secondModel', secondModel);
    var foo = model.get('secondModel.foo');

    assert.equal(foo, 'bar');
  });
});

Tests pass. Demo project repository on GitHub.

like image 74
Daniel Kmak Avatar answered Oct 16 '22 15:10

Daniel Kmak