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 #598

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

Unit Testing Models with Associations/Relationships Fail #598

f3ndot opened this issue Apr 16, 2014 · 3 comments

Comments

@f3ndot
Copy link

f3ndot commented Apr 16, 2014

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.

@stefanpenner
Copy link
Owner

@f3ndot
Copy link
Author

f3ndot commented Apr 16, 2014

Reopened on emberjs/ember-qunit#39

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

2 participants