【问题标题】:Sailjs testing a modelSails Js 测试模型
【发布时间】:2014-03-01 01:42:08
【问题描述】:

我正在尝试为我在 Sails.js 中创建的模型编写一些测试。模型如下图:

/**
 * Users
 *
 * @module      :: Model
 * @description :: A short summary of how this model works and what it represents.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

var User = {

  /**
   * Attributes list for the user
   * @type {Object}
   */
  attributes: {
    password:{
      type:  'string',
      alphanumeric: true,
      required: true,
      minLength: 6
    },
    firstName: {
      type: 'string',
      maxLength: 50,
      minLength: 5,
      required: true
    },
    lastName:{
      type:  'string',
      maxLength: 50,
      minLength: 5,
      required: true
    },
    email: {
      type: 'email',
      unique: true,
      required: true
    }
  },

  /**
   * Function that hashes a password
   * @param  {Object}   attrs - user attributes list
   * @param  {Function} next  [description]
   * @return {[type]}         [description]
   */

  beforeCreate: function( attrs, next ) {
    var bcrypt = require('bcrypt');
    bcrypt.genSalt( 10, function( err, salt ) {
      if ( err ) return next( err );
      bcrypt.hash( attrs.password, salt, function( err, hash ) {
        if ( err ) return next( err );

        attrs.password = hash;
        next();
      });
    });
  }

};

module.exports = User;

我写的测试如下图:

var assert = require('assert')
  , Sails = require('sails')
  , barrels = require('barrels')
  , fixtures;

// Global before hook
before(function (done) {
  // Lift Sails with test database
  Sails.lift({
    log: {
      level: 'error'
    },
    adapters: {
      default: 'mongo'
    }
  }, function(err, sails) {
    if (err)
      return done(err);
    // Load fixtures
    barrels.populate(function(err) {
      done(err, sails);
    });
    // Save original objects in `fixtures` variable
    fixtures = barrels.objects;
  });
});

// Global after hook
after(function (done) {
  console.log();
  sails.lower(done);
});

// User test
describe('Users', function(done) {
  it("should be able to create", function(done) {
    Users.create({firstName: "johnny", lastName: "BeGood", email: "johnnybegood@example.com", password: "validpassword123"}, function(err, user) {
      assert.notEqual(user, undefined);
      done();
    });
  });

  it("should be able to destroy", function(done) {
    Users.destroy({email: "johnnybegood@example.com"}, function(err) {
      assert.equal(true, true);
    });
  });
});

但是,我只能通过第一个测试,我收到的输出如下所示:

我对在 node/sails 中编写测试非常陌生。在这种情况下,谁能指出我做错了什么。我正在使用以下要点 https://gist.github.com/joukokar/57b14e034e41893407f0

【问题讨论】:

    标签: node.js mocha.js sails.js


    【解决方案1】:

    如果您使用的是异步 Mocha 测试,则必须在最后调用回调(done();):

    it("should be able to destroy", function(done) {
      Users.destroy({email: "johnnybegood@example.com"}, function(err) {
        assert.equal(true, true);
    
        done();
      });
    });
    

    另外,我会测试 assert.ifError(err) 而不是 assert.equal(true, true)(如果代码不在那里,您根本不会看到断言,但这并不意味着您实际测试了案例)。

    【讨论】:

    • 谢谢你,我会试试这个。我输入了 true,true 只是为了看看它是否没有给我超时 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2018-05-04
    • 1970-01-01
    • 2014-07-18
    • 2017-01-12
    • 1970-01-01
    • 2018-09-07
    相关资源
    最近更新 更多