ember-cp-validations
ember-cp-validations copied to clipboard
error calling model.validate from Unit test (solved by including validator:ds-error in "needs")
First, thanks for all the great packages :)
When trying to write a unit test for the validation of an ember-model, I stumbled onto getting this error:
Died on test #1 at Anonymous function (http://localhost:7357/assets/tests.js:3207:4) at Module.prototype.exports (http://localhost:7357/assets/vendor.js:92:7) at Module.prototype.build (http://localhost:7357/assets/vendor.js:142:5) at findModule (http://localhost:7357/assets/vendor.js:190:5) at requireModule (http://localhost:7357/assets/vendor.js:177:5) at TestLoader.prototype.require (http://localhost:7357/assets/test-loader.js:67:9) at TestLoader.prototype.loadModules (http://localhost:7357/assets/test-loader.js:58:13): Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.
The error is correct, but not very helpfull in finding the problem, which turns out is that i need to include: 'validator:ds-error' in my needs clause (see test code below).
I am aware that this is not a bug perse, but maybe it can be documented somewhere (better) ? Or maybe the error could be more descriptive...
Environment
- Ember Version: 2.4.5
- Ember CLI Version: 2.5.0
- Ember CP Validations Version: 2.9.8
Steps to Reproduce - Unit test:
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('recipient', 'Unit | Model | recipient', {
// Specify the other units that are required for this test.
needs: [
'validator:ds-error', // this two lines are needed - without it calling validate gives an error !!!
'validator:messages',
'validator:presence',
'validator:length',
'validator:number',
'validator:belongs-to',
'validator:has-many'
]
});
test('it exists', function(assert) {
let model = this.subject();
assert.ok(!!model);
});
test('it can validate', function(assert) {
let model = this.subject();
model.validate();
assert.ok(true);
});
Appareantly there is more going on - because it looked like i fixed - the error - but now its back again ... I will come back after i investigated and have better understanding. Sorry for the noise ...
I think its fixed when i include both: 'validator:messages' 'validator:ds-error'
(I edited the example above)
FWIW, both needs and unit options with ember-qunit (and ultimately Ember-mocha) will be deprecated soon.
See https://github.com/emberjs/rfcs/blob/master/text/0229-deprecate-testing-restricted-resolver.md for more details.
@rwjblue should I convert the default unit test blueprint to be an integration test then?