javascript - 使用 Mocha 在 NodeJS 和 MongoDB 中测试用户模型时没有结果

标签 javascript node.js mongodb testing mocha.js

我正在用 mocha 测试我的模型。我有以下文件:

测试/utils.js

'use strict';
//  Modified from https://github.com/elliotf/mocha-mongoose

var config = require('../config/db.js');
var mongoose = require('mongoose');

// ensure the NODE_ENV is set to 'test'
// this is helpful when you would like to change behavior when testing
process.env.NODE_ENV = 'test';

beforeEach(function (done) {

  function clearDB() {
    for (var i in mongoose.connection.collections) {
      mongoose.connection.collections[i].remove(function() {});
    }
    return done();
   }

  if (mongoose.connection.readyState === 0) {
    mongoose.connect(config.url, function (err) {
      if (err) {
        throw err;
      }
      return clearDB();
    });
  } else {
     return clearDB();
  }
});


afterEach(function (done) {
  mongoose.disconnect();
  return done();
});

test/testUserModel.js

'use strict';


// import the moongoose helper utilities
var utils = require('../test/utils');
var should = require('should');
// import our User mongoose model
var User = require('../app/models/user.js');

var testUser1 = new Object({ 
    profilePic: "testPic",
    email: "testEmail",
    first_name: "fname",
    last_name: "lname",
    description: "description",
    personality: "personality",
    phone_number: "phoneNum",
    password: "password",
    courses: {
      course_name: "courseTest"
      },
    role: "testRole" //student/admin/teacher
});

var testUser2 = new Object({ 
    profilePic: "testPic2",
    email: "testEmail2",
    first_name: "fname2",
    last_name: "lname2",
    description: "description2",
    personality: "personality2",
    phone_number: "phoneNum2",
    password: "password2",
    courses: {
      course_name: "courseTest2"
      },
    role: "testRole2" //student/admin/teacher
});

describe('Users: models', function () {

describe('#register()', function () {
    it('should register a new User', function (done) {
      User.register(testUser1, function (err, createdUser) {
        should.not.exist(err);
        createdUser.email.should.equal("testEmail");
        createdUser.first_name.should.equal("fname");
        createdUser.phone_number.should.equal("phoneNum");
        createdUser.password.should.equal("password");
        createdUser.role.should.equal("testRole");
        done();
      });
    });
  });

  describe('#getAllUsers', function () {

    it('should return all users in DB', function (done) {

      console.log("about to add stuff");

      User.register(testUser1, function (err, createdUser) {
          console.log(createdUser);
      });

      User.register(testUser2, function (err, createdUser) {
        console.log(createdUser);
      });

      User.getAllUsers(function (err, createdUsers) {
        should.not.exist(err);
        createdUsers.should.be.instanceof(Array).and.have.lengthOf(2);
        done();
      });  
    });
  });
});

我已经测试了用户的注册功能,并且在测试#register() 中工作正常, 但是在#getAllUsers() 中,我对两个用户对象 testUser1 和 testUSer2 进行了注册。但是我的 User.getAllUsers 函数没有从数据库返回任何内容。在部署中,这两个功能运行良好,您可以添加大量用户,它们显示在 GUI 的列表中。似乎在 User.register 函数调用之后,数据库被清除了,所以当它到达 User.getAllUsers 时,那里什么也没有。有什么想法吗?

最佳答案

使用您的代码:

  describe('#getAllUsers', function () {

it('should return all users in DB', function (done) {

  console.log("about to add stuff");

  User.register(testUser1, function (err, createdUser) {
      console.log(createdUser); // register was successfull
  });

  User.register(testUser2, function (err, createdUser) {
    console.log(createdUser); //register was successfull
  });

// Here, it should set in callback, when register function was successfull
  User.getAllUsers(function (err, createdUsers) {
    should.not.exist(err);
    createdUsers.should.be.instanceof(Array).and.have.lengthOf(2);
    done();
  });  
});

你可以看到注册成功的例子如下:

it('should return all users in DB', function (done) {

  console.log("about to add stuff");

  User.register(testUser1, function (err, createdUser) {
      console.log(createdUser); 
      // here, it should set in callback, when register is successfull
      User.getAllUsers(function (err, createdUsers2) {
         should.not.exist(err);
         createdUsers2.should.be.instanceof(Array).and.have.lengthOf(1);
         done();
      });
  });

});

关于javascript - 使用 Mocha 在 NodeJS 和 MongoDB 中测试用户模型时没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769174/

相关文章:

java - 如何将查询存储在 MongoDB 集合中以便动态执行它们?

javascript - 使用 JavaScript 读取 INI 文件

javascript - Angular/Ui-Grid (ng-grid)/angular 未定义

ruby - Mongoid:从父级上的嵌入式文档运行回调

node.js - node_redis : is SMEMBERS blocking?

node.js - 将变量从 jade 文件传递​​到 React 组件

javascript - Node.js 从 mongodb 渲染 json 映射

javascript - 如何在 React with Redux 项目中实现用于 API 服务调用的动态 reducer 创建器?

javascript - 如何通过调整图像大小和应用 attr 来减少页面加载时间?

javascript - 在全局空间中运行代码与在 Javascript 函数中运行代码的性能