ember-data-model-fragments icon indicating copy to clipboard operation
ember-data-model-fragments copied to clipboard

Unit test failure using Mocha : Error: Assertion Failed: (subclass of DS.ModelFragment) does not appear to be an ember-data model

Open jeep87c opened this issue 9 years ago • 4 comments

Using Ember-cli setup with mocha/chai as my test runner and assertions lib (using ember-cli-mocha), I have a simple model class looking like this :

models/Address.js

import DS from "ember-data";

export default DS.ModelFragment.extend({
    civicNumber: DS.attr('number'),
    streetName: DS.attr('string'),
    city: DS.attr('string'),
    provinceOrState: DS.attr('string'),
    country: DS.attr('string'),
    postalZipCode: DS.attr('string'),
    building: DS.belongsTo('building', {embedded: 'always'})
});

With the following test code :

/* jshint expr:true */
import { expect } from 'chai';
import {
  describeModel,
  it
} from 'ember-mocha';

describeModel(
  'address',
  'Address',
  {
    // Specify the other units that are required for this test.
      needs: []
  },
  function() {
    // Replace this with your real tests.
    it('exists', function() {
      var model = this.subject();
      // var store = this.store();
      expect(model).to.be.ok;
    });
  }
);

Which fails with the following error : not ok 14 PhantomJS 1.9 - Address exists --- message: > Assertion Failed: (subclass of DS.ModelFragment) does not appear to be an ember-data model stack: > Error: Assertion Failed: (subclass of DS.ModelFragment) does not appear to be an ember-data model at http://localhost:7357/assets/vendor.js:14489 at http://localhost:7357/assets/vendor.js:74951 at http://localhost:7357/assets/vendor.js:73546 at http://localhost:7357/assets/test-support.js:2092 at http://localhost:7357/assets/vendor.js:10749 at run (http://localhost:7357/assets/vendor.js:27187) at http://localhost:7357/assets/test-support.js:2093 at http://localhost:7357/assets/test-support.js:2299 at http://localhost:7357/assets/frontend.js:1104 at http://localhost:7357/assets/test-support.js:1686 at invoke (http://localhost:7357/assets/test-support.js:13702) at http://localhost:7357/assets/test-support.js:13767 at http://localhost:7357/assets/test-support.js:7004 at http://localhost:7357/assets/test-support.js:7423 at http://localhost:7357/assets/test-support.js:7514 at next (http://localhost:7357/assets/test-support.js:7348) at http://localhost:7357/assets/test-support.js:7358 at next (http://localhost:7357/assets/test-support.js:7297) at http://localhost:7357/assets/test-support.js:7320 at done (http://localhost:7357/assets/test-support.js:6983) at http://localhost:7357/assets/test-support.js:7003 at complete (http://localhost:7357/assets/test-support.js:13729) at http://localhost:7357/assets/test-support.js:13707 at tryCatch (http://localhost:7357/assets/vendor.js:60990) at invokeCallback (http://localhost:7357/assets/vendor.js:61002) at publish (http://localhost:7357/assets/vendor.js:60973) at http://localhost:7357/assets/vendor.js:39112 at http://localhost:7357/assets/vendor.js:11403 at http://localhost:7357/assets/vendor.js:11468 at http://localhost:7357/assets/vendor.js:11273 at http://localhost:7357/assets/vendor.js:10698 at http://localhost:7357/assets/vendor.js:11101 Log: > ...

Am I missing something or it's a real issue? Any help will be highly appreciated! Thanks

jeep87c avatar May 10 '15 22:05 jeep87c

This is likely due to isolatedContainer issues as described in #80. It would be useful to know what version of ED you are using. A small ember-cli repo reproducing the issue would be even more useful :wink:

Also keep in mind that relationships to models are not currently supported.

slindberg avatar May 11 '15 17:05 slindberg

Thanks for you answer! I'm using ED 1.0.0-beta.16.

jeep87c avatar May 17 '15 01:05 jeep87c

This doesn't seem to have been resolved by #80 - I have the same error using ember-qunit's moduleForModel('address').

It looks like this.subject() is calling store.createRecord('address') instead of store.createFragment('address').

// Ember           : 1.12.0
// Ember Data      : 1.0.0-beta.18
// jQuery          : 2.1.4
// Model Fragments : 0.3.3

Uncaught Error: Assertion Failed: `(subclass of DS.ModelFragment)` does not appear to be an ember-data model
    Ember.default.assert @ ember.debug.js:4854
    ember$data$lib$system$store$$Service.extend.buildRecord @ store.js:41
    ember$data$lib$system$store$$Service.extend.createRecord @ store.js:661
    (anonymous function) @ test-module-for-model.js:36
    Backburner.run @ ember.debug.js:224
    run @ ember.debug.js:15863
    exports.default.TestModule.default.extend.setupModel.callbacks.subject @ test-module-for-model.js:35
    exports.default.klassy.Klass.extend.contextualizeCallbacks.context.(anonymous function) @ test-module.js:201
    (anonymous function) @ address-test.js:11
    wrapper @ test.js:9
    Test.run @ qunit.js:879
    (anonymous function) @ qunit.js:1014
    process @ qunit.js:573
    begin @ qunit.js:618
    (anonymous function) @ qunit.js:634

Truffula avatar Jun 19 '15 03:06 Truffula

That's what was going on, make more sense now. I must confess, I don't unit test model fragments in our app; we are rely heavily on acceptance testing. I'm not sure what the right answer is here, but for now the easiest thing to do is to call store.createFragment() directly instead of using this.subject(). At some point perhaps I'll try to monkey patch moduleForModel, or perhaps create a moduleForFragment method, although neither of those options sits well with me.

slindberg avatar Jun 19 '15 16:06 slindberg