我的 mocha 测试单独运行,但同时运行时失败

nak*_*nak 5 mocha.js mongoose node.js coffeescript

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

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

另外:我对这个软件测试的东西非常陌生,如果有人有任何提示,或者想批评我公然的错误,请随时给我发电子邮件(查看我的个人资料)

谢谢!!

这是我的两个测试文件(咖啡脚本):

/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()
Run Code Online (Sandbox Code Playgroud)

/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()
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

nak*_*nak 1

我发现由于某种原因,public.username与 一起运行测试时索引被删除npm test。但在单独运行每个测试时被维护。

我在 中更改了以下内容test/models.coffee

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, done
Run Code Online (Sandbox Code Playgroud)

至以下内容:

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, ->
      User.collection.ensureIndex { 'public.username': 1 }, { unique: true }, (err) ->
        throw err if err
        done()
Run Code Online (Sandbox Code Playgroud)

...它调用EnsureIndex一个内部猫鼬函数,该函数在最初编译模型时调用,但在测试的生命周期中由于某种原因被删除。

使用猫鼬 3.5.4