0

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>

Animal Rights
  • 9,107
  • 6
  • 28
  • 40
  • which version of ember-validations library you are using? – kushdilip May 27 '15 at 06:53
  • When I ran `ember install ember-validations`, it added this to my package.json: "ember-validations": "2.0.0-alpha.3" – Animal Rights May 27 '15 at 06:54
  • do you get that error when running the test or during build. – kushdilip May 27 '15 at 07:20
  • Well when I didn't import EmberValidations, I got an error saying validate is not a function. Once I did the import and the mixin correclty, that error went away, so I think the library is loading ok, but then the most basic example isn't doing what I expect. Now I did see this: https://github.com/dockyard/ember-validations#testing but that resulted in more errors so maybe I'm doing that setup incorrectly? – Animal Rights May 27 '15 at 07:44

1 Answers1

1

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.

Kuba Niechciał
  • 974
  • 7
  • 9
  • thanks. looks like it solved that warning, but now I get another cryptic error that says: Died on test #1 at Object.test (http://localhost:4200/assets/test-support.js:1644:11) ...... at Object.TestLoader.loadModules (http://localhost:4200/assets/test-loader.js:21:18): <(unknown mixin):ember848>. Any idea? – Animal Rights May 27 '15 at 16:00
  • Add a full code of your spec to your questions (including `module` definiton, `imports`, etc.) – Kuba Niechciał May 27 '15 at 16:03