Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing models with associations/relationships fail #39

Closed
f3ndot opened this issue Apr 16, 2014 · 6 comments
Closed

Unit testing models with associations/relationships fail #39

f3ndot opened this issue Apr 16, 2014 · 6 comments

Comments

@f3ndot
Copy link

f3ndot commented Apr 16, 2014

[ This refers to v0.1.7 ]

When unit testing a model that makes use of DS.belongsTo() the createRecord() fails with the following error:

Error: No model was found for 'Client'

It appears that in order to create a record of a model, it expects its associated or related model to already exist. What's going on?


Here are the models and test

/app/models/client.js:

export default DS.Model.extend({
  name: DS.attr('string'),
  invoices: DS.hasMany('Invoice'),
});

/app/models/invoice.js:

export default DS.Model.extend({
  amount: DS.attr('number'),
  discountTotal: DS.attr('number'),

  totalAmount: function() {
    return this.get('amount') - this.get('discountTotal');
  }.property('amount', 'discountTotal'),

  client: DS.belongsTo('Client'),
});

tests/models/invoice.js:

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

moduleForModel('invoice', 'Invoice Model');

test('totalAmount computed property considers discounts', function() {
  // this.subject() is aliased more or less to createRecord() for the
  // invoice model. See module-for-model.js in ember-qunit.
  //
  // Attempting to run the below function throws the error.
  var invoice = this.subject({
    id: 1,
    amount: 50.0
    discountTotal: 10.0
  });

  equal(invoice.get('totalAmount'), 40.0);
});
@f3ndot
Copy link
Author

f3ndot commented Apr 16, 2014

It's worth mentioning that there are no FIXTURES array defined for the models, since thus far testing I've done has been using an api-stub server in integration testing.

Not sure if that's a contributing factor.

@pangratz
Copy link
Member

You need to add needs: ["model:client"] to your moduleForModel, see http://emberjs.jsbin.com/tajegefa/1/edit:

moduleForModel('invoice', 'Invoice Model', {
  needs: ["model:client"]
});

Also the naming convention is to specify the classes for the relationship with lowerCamelCase, so it's client and not Client.

@stefanpenner
Copy link
Member

the "needs" model will by somewhat crippled though, since it wont be setup correctly. But it should work for simple unit tests.

@f3ndot
Copy link
Author

f3ndot commented Apr 16, 2014

@pangratz Adding needs: [] has no effect.

@f3ndot
Copy link
Author

f3ndot commented Apr 16, 2014

@pangratz Ah, I just saw your edit about naming conventions. I saw some example code that used TitleCase and that tripped me up.

Changing to camelCase, so DS.belongsTo('client') in this case, fixes the problem.

Thank you!

@Subodha555
Copy link

**import { moduleForModel, test } from 'ember-qunit';
import Ember from 'ember';
moduleForModel('trade/business-entities/order-params','Order-params-test',{

});

test('ordVal', function() {

var ctrl = this.subject();

assert.equal(ctrl.get('ordSide'),1,'should be 1');
});**

This is my test code. I get following error when I run this.

beforeEach failed on ordVal: WebSocketManager.default is not a constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants