question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing models with associations/relationships fail

See original GitHub issue

[ 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);
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
pangratzcommented, Apr 16, 2014

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.

0reactions
Subodha555commented, Jul 13, 2016

**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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby on rails - Should I test associations between models?
No, you don't need to specifically unit-test associations, even if your goal is to completely test your application (which you should).
Read more >
How to Test Rails Models with RSpec - Semaphore CI
By the end of this tutorial you will know how to test your Rails models using RSpec and the Behaviour-driven Development approach.
Read more >
Examples of pointless types of RSpec tests - Code with Jason
Here are some examples from the above gist of association tests: ... There's negligible value in simply testing that a model responds to...
Read more >
Unit-Testing Sequelize Models Made Easy - ITNEXT
The mock sequelize object uses sinon to attach spies to each of Sequelize's association types, meaning you can test them by invoking Model....
Read more >
Testing best practices - GitLab Documentation
GitLab development guidelines - testing best practices. ... bin/rspec spec/models/project_spec.rb -e associations # run all tests, ... Fast unit tests.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found