Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-cli : relationship unit test for model failing

I am using Ember-cli and qunit for testing.

Item Model

import DS from 'ember-data';
var attr = DS.attr,
    belongsTo = DS.belongsTo;

export default DS.Model.extend({
  offer: belongsTo('offer'),
});

Here am adding test for relation between item and Offer model.

Item Tests

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

moduleForModel('item', 'Item Model', {
  needs: ['model:item']
});

test('offer relationship', function() {
  var relationships = Ember.get(App.Item, 'relationships');
  deepEqual(relationships.get(App.Offer), [
    { name: 'offer', kind: 'belongsTo' }
  ]);
});

Error Trace:

Died on test #1     at test (http://localhost:4200/assets/vendor.js:73836:13)
    at eval (goodcity/tests/unit/item-test.js:44:5)
    at requireModule (http://localhost:4200/assets/vendor.js:54:29)
    at http://localhost:4200/assets/test-loader.js:14:29: App is not defined
Source:     
ReferenceError: App is not defined
    at Object.eval (goodcity/tests/unit/item-test.js:45:37)
    at Object.wrapper (http://localhost:4200/assets/vendor.js:73824:31)
    at Object.Test.run (http://localhost:4200/assets/qunit.js:203:18)
    at http://localhost:4200/assets/qunit.js:361:10
    at process (http://localhost:4200/assets/qunit.js:1453:24)
    at http://localhost:4200/assets/qunit.js:479:5

Am i missing something?

like image 533
Swati Avatar asked Dec 20 '25 02:12

Swati


1 Answers

I'm just now in the process of converting over an older Ember App to the new ember-cli and ran into a similar situation. Since Ember CLI uses ES6 module syntax you can't access anything directly on the App object.

You will need to import your objects from their respective model files.

import Item from "<modulePrefix>/models/item";
import Offer from "<modulePrefix>/models/offer";

Second, your moduleForModel('item') should have needs: ['model:offer'].

Here's a passing a test using the provided files. (I used: ember new stackoverflow)

import Ember from "ember";
import { test, moduleForModel } from 'ember-qunit';
/* Import Models */
import Item from "stackoverflow/models/item";
import Offer from "stackoverflow/models/offer";

moduleForModel('item', 'Item', {
  // Item needs the offer model.
  needs: ['model:offer']
});

test('offer relationship', function() {
  /* For some reason this was necessary to prime the store. */
  /* Without this line I get the error:  
     'undefined' is not an object (evaluating 'store.modelFor') */
  var model = this.subject();

  /* App.Item -> Item, App.Offer -> Offer */
  var relationships = Ember.get(Item, 'relationships');
  deepEqual(relationships.get(Offer), [
    { name: 'offer', kind: 'belongsTo' }
  ]);
});
like image 123
jheth Avatar answered Dec 22 '25 17:12

jheth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!