node.js - 我的 mocha 测试单独工作,但同时运行时失败

标签 node.js coffeescript mongoose mocha.js

这可能与异步代码有关,但我不确定是什么。当我将它们分开运行时,两者都通过了:mocha test/models.coffeemocha test/login.coffee

但是 describe 'saving another user with same username', -> 当我与 npm test 一起运行测试时失败。我想象数据库在运行该步骤时会被清除,因此它会保存而不是产生错误,因为它应该是一个唯一值。

此外:我在软件测试方面非常陌生,如果有人有任何提示,或者想批评我的明显错误,请随时给我发电子邮件(查看我的个人资料)

谢谢!!

这是我的两个测试文件(coffeescript):

/test/models.coffee

should = require 'should'
{User} = require '../models'

# function to find our test user, John Egbert
findJohn = (cb) ->
  User.findOne {'public.username': 'john'}, (err, john) ->
    throw err if err
    cb(john)

before (done) ->

  # Drop all users from database
  User.collection.drop()

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, done

describe 'create user', ->
  it 'should create a document that can be found', (done) ->
    findJohn (user) ->
      should.exist user
      done()

describe 'user password', ->
  it 'should NOT be stored in plaintext', (done) ->
    findJohn (user) ->
      user.password.should.not.eql 'test123'
      done()

  it 'should return true for matching password', (done) ->
    findJohn (user) ->
      user.comparePassword 'test123', (err, isMatch) ->
        isMatch.should.eql true
        done()

  it 'should return false for non-matching password', (done) ->
    findJohn (user) ->
      user.comparePassword 'wrong_password', (err, isMatch) ->
        isMatch.should.not.eql true
        done()

describe 'saving another user with same username', ->
  it 'should produce an error', (done) ->
    User.create public: {username: 'john'}, (err) ->
      should.exist err
      done()

/test/login.coffee:

should = require 'should'
{User} = require '../models'
login = require '../services/login'

before (done) ->

  # Drop all users from database
  User.collection.drop()

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, done

describe 'login', ->
  it 'should return true for an existing username/password combo', (done) ->
    login username: 'john', password: 'test123', (err, loggedIn) ->
      should.not.exist(err)
      loggedIn.should.be.true
      done()

  it 'should return false for a bad username/password combo', (done) ->
    login username: 'john', password: 'wrong_pass', (err, loggedIn) ->
      should.not.exist(err)
      loggedIn.should.be.false
      done()

/models.coffee

fs = require 'fs'
path = require 'path'
mongoose = require 'mongoose'

#Connect to mongodb
#TODO: env variable to choose production/development/testing databases
mongoose.connect 'localhost', 'siglr'

models = {}

for file in fs.readdirSync './schemas'
  if path.extname(file) is '.coffee'
    modelName = path.basename file, '.coffee'
    schema = require "./schemas/#{modelName}"
    models[modelName] = mongoose.model modelName, schema

# key is model name, value is actual mongoose model
module.exports = models

最佳答案

有一件事可能会造成让您发疯的竞争条件。删除用户集合时,您没有传递回调。我不能确定这是否会导致问题,例如在删除集合之前插入测试用户,但这种未能正确使用回调以等待异步操作完成的模式是您的程序的一个秘诀以难以调试的方式行为不端。试试这个:

before (done) ->

    # Drop all users from database
    User.collection.drop (err) ->

        # Create our test user, his username is John Egbert
        User.create
            password: 'test123'
            public: {username: 'john'}, done

第二个建议:将 console.log 语句放在每个 describe/before/it 的开头(第一个语句),另一个紧接在调用 done 之前() 并查看它们是否完全按照您期望的顺序出现。

关于node.js - 我的 mocha 测试单独工作,但同时运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14113550/

相关文章:

node.js - Npm 肯定不行

javascript - console.log ('You said ${reply}' 的输出问题;

javascript - 正则表达式匹配字符串的前 n 个字符

node.js - MongoDB 返回空数组

node.js - 与 Mongoose 的奇怪约会行为

php - Node js调用socket.io函数的用法

java - Java 版 CoffeeScript

inheritance - CoffeeScript继承;基类中的数组变为静态?

backbone.js - Underscore.js "some"不返回 bool 值

javascript - MongoDB 对填充字段的查询