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

1

17

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');
  });
});
Seidler answered 6/3, 2015 at 19:11 Comment(4)
You seem to be using obsolete versions of Ember Data.Bonedry
Why do you say that?Seidler
which ember-data version you use? I'm experiencing the same problem during integration tests under beta15Balderdash
Yep, I was on beta15. From what I recall, this was because I had setup my models to persist any property changes immediately, rather than waiting until save was called, similar to this. I forget how I resolved the issue, though.Seidler
C
3

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.

Copyboy answered 27/9, 2015 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.