我的摩卡testing分开工作,但一次运行全部失败

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

但是, describe 'saving another user with same username', ->npm test一起运行testing失败。 我想数据库得到清除,因为它正在运行该步骤,所以它节省了,而不是产生一个错误,因为它应该是一个独特的价值。

另外:在这个软件testing方面我是非常新的,如果有人有任何提示,或者想批评我公然的错误,随时给我发电子邮件(检查我的个人资料)

谢谢!!

这里是我的两个testing文件(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 

这是一件可能造成竞争的事情,会让你发疯。 删除用户集合时,不会传递callback。 我不能肯定地说这是否会导致一个问题,例如testing用户在集合被删除之前被插入,但是这种无法正确使用callback来等待asynchronous操作完成的模式是您的程序以难以debugging的方式行事不端。 尝试这个:

 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 

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

我发现,由于某些原因,当与npm test一起运行testing时, public.username索引被删除。 但是在单独运行每个testing时都保持不变。

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

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

如下:

  # 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() 

…它调用ensureIndex一个内部的mongoose函数,这个函数在最初编译模型的时候被调用,但是在testing的生命周期中由于某种原因被删除。

使用Mongoose 3.5.4