mercredi 27 juillet 2016

Unit Test for custom validations is giving error

I'm doing some unit tests on Ember-Cli but I'm getting an error. I'm using ember-cp-validations and created a custom validation. After creating the custom validator and running the test this is the error I got: (Error: No model was found for 'userlog'), but I have that model. I can't find what I'm missing here.

Here is my model userlog.js with the validations:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
    username: [ 
        validator('presence', true),
        validator('format', { type: 'email' }),
        validator('corporate-email', { showSuggestions: true})
    ],
  password: [
    validator('presence', true),
    validator('length', {
      min: 6,
      max: 40
    })
  ]
});

export default Model.extend(Validations, {
  username: attr('string'),
  password: attr('string')
});

This is my corporate-email.js validator

import Ember from 'ember';
import BaseValidator from 'ember-cp-validations/validators/base';


const CorporateEmail = BaseValidator.extend({

// To interact with the store within the validator, the service has to be injected.
  store: Ember.inject.service(),
  validate(value) {

    return this.get('store').query('userlog',{username: value}).then(result=>{

      if (result.match("yahoo") || result.match("gmail") || result.match("hotmail")){
        return false;
      } else {
        return true;
      }
    });

  }

});

CorporateEmail.reopenClass({
  /**
   * Define attribute specific dependent keys for your validator
   *
   * @param {String}  attribute   The attribute being evaluated
   * @param {Unknown} options     Options passed into your validator
   * @return {Array}
   */
  getDependentsFor(/* attribute, options */) {
    return [];
  }
});

export default CorporateEmail;

This is my corporate-email-test.js

import { moduleFor, test } from 'ember-qunit';


moduleFor('validator:corporate-email', 'Unit | Validator | corporate-email', {
    needs: ['validator:messages']
});

test('it works', function(assert) {
    var validator =  this.subject();
    assert.ok(validator);
});

test('username email is not corporate', function(assert) {
    let validator =  this.subject();
    let done = assert.async();

    validator.validate('name@yahoo.com').then((message) => {
      assert.equal(message, false);
      done();
    });

});

What could be the problem?.Where can I find more information about unit testing(I read the ember-cli documentation already).

Thanks for your time

Aucun commentaire:

Enregistrer un commentaire