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:
- Created 9 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You need to add
needs: ["model:client"]
to yourmoduleForModel
, see http://emberjs.jsbin.com/tajegefa/1/edit:Also the naming convention is to specify the classes for the relationship with lowerCamelCase, so it’s
client
and notClient
.**import { moduleForModel, test } from ‘ember-qunit’; import Ember from ‘ember’; moduleForModel(‘trade/business-entities/order-params’,‘Order-params-test’,{
});
test(‘ordVal’, function() {
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