Could not find the "presence" validator with Ember Validations addon - ember.js

I'm trying to use the Ember Validations addon and I can't get it working. In the Chrome console, I see: WARNING: Could not find the "presence" validator.
Here is my model:
import Ember from 'ember';
import DS from 'ember-data';
import EmberValidations from 'ember-validations';
export default DS.Model.extend(EmberValidations.Mixin, {
name: DS.attr('string'),
validations: {
name: {
presence: true
}
}
});
And here is the my test:
import Ember from 'ember';
import EmberValidations from 'ember-validations';
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('person', 'Unit | Model | person', {
// Specify the other units that are required for this test.
needs: ['ember-validations#validator:local/presence'],
afterEach: function() {
window.sessionStorage.clear();
}
});
test('isValid should be false if name is not set', function(assert) {
stop();
var model = this.subject();
console.log(model);
Ember.run(function() {
sinon.spy(model, 'save');
model.validate().then(function() {
start();
assert.equal(model.get('isValid'), false);
});
});
});
The result of this test is:
Died on test #1 at Object.test (http://localhost:4200/assets/test-support.js:1644:11)
at http://localhost:4200/assets/myproj.js:14450:15
at mod.state (http://localhost:4200/assets/vendor.js:150:29)
at tryFinally (http://localhost:4200/assets/vendor.js:30:14)
at requireModule (http://localhost:4200/assets/vendor.js:148:5)
at Object.TestLoader.require (http://localhost:4200/assets/test-loader.js:29:9)
at Object.TestLoader.loadModules (http://localhost:4200/assets/test-loader.js:21:18): <(unknown mixin):ember848>

You need to add dependencies to test suite as defined in ember-validations docs in testing part. However, take notice that these docs are a little bit outdated. The proper needs should include only the validators you use (presence) and look like this:
needs: ['ember-validations#validator:local/presence']
Do not indclude service:validations.
I posted an issue quite a while ago but it's not discussed yet.

Related

Testing route with unauthenticatedRouteMixin fails with "Promise rejected before it exists"

THE ROUTE
import Ember from "ember";
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';
const {
Route,
} = Ember;​
export default Route.extend(UnauthenticatedRouteMixin, {
});
THE TEST
import { moduleFor, test } from 'ember-qunit';​
moduleFor('route:index', 'Unit | Route | index', {});
test('it exists', function(assert) {
var route = this.subject();
assert.ok(route);
});
THE ERROR
Promise rejected before it exists: Could not find module `ember-simple-auth/mixins/unauthenticated-route-mixin` imported from `appname/routes/index`
Ember: 1.13.15
Ember Simple Auth: 1.0.1
It is just the standard test generated by ember but fails everytime.
Thank you for any help

Access model from acceptance test

How do I access the current model? I am aware of application.__container_.lookup but I understand this is a bit of a hack.
import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'myapp/tests/helpers/start-app';
let application;
module('Acceptance | booking/edit', {
beforeEach: function() {
application = startApp();
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('visiting /booking/edit', function(assert) {
visit('/booking/1');
//At this point I would like to access the model returned from the route model hook.
andThen(function() {
assert.equal(currentURL(), '/booking/1');
});
});
Sample Route excerpt.
this.route('booking', { path:'/booking' }, function() {
this.route('edit', { path:'/:booking_id' }, function() {
this.route('account', { path:'/account' });
...
});
...
});
You should be able to use moduleFor and then within the test you can use this.subject() to access the controller.
moduleFor('controller:bookingsEdit', 'Bookings Edit Controller');
If moduleFor is undefined. Then import moduleFor import {moduleFor} from 'ember-qunit';
and then within the test you can use this.subject() to access the controller
moduleFor(fullName [, description [, callbacks]])
fullName: (String) - The full name of the unit, ie
controller:application, route:index.
description: (String) optional - The description of the module
callbacks: (Object) optional - Normal QUnit callbacks (setup and
teardown), with addition to needs, which allows you specify the other
units the tests will need.
http://guides.emberjs.com/v1.10.0/testing/testing-controllers/
https://github.com/rwjblue/ember-qunit

Ember-cli : relationship unit test for model failing

I am using Ember-cli and qunit for testing.
Item Model
import DS from 'ember-data';
var attr = DS.attr,
belongsTo = DS.belongsTo;
export default DS.Model.extend({
offer: belongsTo('offer'),
});
Here am adding test for relation between item and Offer model.
Item Tests
import Ember from "ember";
import DS from "ember-data";
import { test, moduleForModel } from 'ember-qunit';
moduleForModel('item', 'Item Model', {
needs: ['model:item']
});
test('offer relationship', function() {
var relationships = Ember.get(App.Item, 'relationships');
deepEqual(relationships.get(App.Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});
Error Trace:
Died on test #1 at test (http://localhost:4200/assets/vendor.js:73836:13)
at eval (goodcity/tests/unit/item-test.js:44:5)
at requireModule (http://localhost:4200/assets/vendor.js:54:29)
at http://localhost:4200/assets/test-loader.js:14:29: App is not defined
Source:
ReferenceError: App is not defined
at Object.eval (goodcity/tests/unit/item-test.js:45:37)
at Object.wrapper (http://localhost:4200/assets/vendor.js:73824:31)
at Object.Test.run (http://localhost:4200/assets/qunit.js:203:18)
at http://localhost:4200/assets/qunit.js:361:10
at process (http://localhost:4200/assets/qunit.js:1453:24)
at http://localhost:4200/assets/qunit.js:479:5
Am i missing something?
I'm just now in the process of converting over an older Ember App to the new ember-cli and ran into a similar situation. Since Ember CLI uses ES6 module syntax you can't access anything directly on the App object.
You will need to import your objects from their respective model files.
import Item from "<modulePrefix>/models/item";
import Offer from "<modulePrefix>/models/offer";
Second, your moduleForModel('item') should have needs: ['model:offer'].
Here's a passing a test using the provided files. (I used: ember new stackoverflow)
import Ember from "ember";
import { test, moduleForModel } from 'ember-qunit';
/* Import Models */
import Item from "stackoverflow/models/item";
import Offer from "stackoverflow/models/offer";
moduleForModel('item', 'Item', {
// Item needs the offer model.
needs: ['model:offer']
});
test('offer relationship', function() {
/* For some reason this was necessary to prime the store. */
/* Without this line I get the error:
'undefined' is not an object (evaluating 'store.modelFor') */
var model = this.subject();
/* App.Item -> Item, App.Offer -> Offer */
var relationships = Ember.get(Item, 'relationships');
deepEqual(relationships.get(Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});

Ember.js - Integration Testing with Ember-CLI

I am following a Dockyard Tutorial on using ember-cli with rails. This particular section is on basic integration testing. Unfortunately, it doesn't seem to be registering properly (at least I don't think so). The test should fail and say something along the lines of "Expected: 'Welcome to Boston Ember'"; rather, it says "should pass jshint" which it is and is therefore passing. Any idea what I am doing wrong?
tests/integration/landing-page-test.js
import Ember from 'ember';
import startApp from 'bostonember/tests/helpers/start-app';
var App;
module('Integration - Landing Page', {
setup: function() {
App = startApp();
},
teardown: function() {
Ember.run(App, 'destroy');
}
});
test('Should welcome me to Boston Ember', function() {
visit('/').then(function() {
equal(find('h2#title').text(), 'Welcome bloopde bloopasa to Boston Ember');
});
});
tests/helpers/start-app.js
/* global require */
var Application = require('bostonember/app')['default'];
var Router = require('bostonember/router')['default'];
import Ember from 'ember';
export default function startApp(attrs) {
var App;
var attributes = Ember.merge({
// useful Test defaults
rootElement: '#ember-testing',
LOG_ACTIVE_GENERATION:false,
LOG_VIEW_LOOKUPS: false
}, attrs); // but you can override;
Router.reopen({
location: 'none'
});
Ember.run(function(){
App = Application.create(attributes);
App.setupForTesting();
App.injectTestHelpers();
});
App.reset(); // this shouldn't be needed, i want to be able to "start an app at a specific URL"
return App;
}
Will provide additional info upon request. Thank you!
Must have just been a hiccup?
Solution was to recreate the file, same code, but works now.

Testing an ember-data model - can't find relationship

I am trying to test a model relationship in an ember-cli application but it keeps telling me: No model was found for 'rateType'. It appears that it can't find my models.
Files
~app/models/account.js
~app/models/rate-type.js
Account Model
export default DS.Model.extend({
...
rateType: DS.belongsTo('rateType'),
});
Test
import Ember from 'ember';
import { test, moduleForModel } from 'ember-qunit';
import Account from 'app/models/account';
import RateType from 'app/models/rate-type';
moduleForModel('account', 'Account Model', {
// Specify the other units that are required for this test.
needs: ['model:rate-type']
});
test('rateType relationship', function() {
expect(0);
this.subject(); //error here
// var relationships = Ember.get(Account, 'relationships');
// deepEqual(relationships.get('rate-type'), [
// { name: 'rateType', kind: 'belongsTo' }
// ]);
});
I have tried camel casing the needs attribute butit does not like that at all.
needs: ['model:rateType', 'model:fuelGroup']
I think what you need is the needs keyword:
moduleForModel('post', 'Unit | Model | post', {
needs: ['model:comment', 'model:user']
});
I found it in the docs here: http://guides.emberjs.com/v1.10.0/testing/testing-models/
Your issue is with the model. Try dasherizing 'rate-type' in the belongsTo relationship.
export default DS.Model.extend({
...
rateType: DS.belongsTo('rate-type')
});