我有一个Mocha测试在第一次运行时正常运行,但是当进行更改并且Mocha第二次重新运行测试时(使用--watch
标志),它会失败。我的测试:
var mongoose = require('mongoose')
, chai = require('chai')
, request = require('supertest')
, _ = require('underscore')
, app = require('../../app').boot('testing')
, Link = mongoose.model('Link')
, should = chai.should()
request = request(app)
describe('Links resource', function () {
var userId = '512298b46271845564000002'
, linkId
, sampleLink = {
'uri': 'http://1.com/',
'meta': {
'title': 'Link 1',
'desc': 'Lorem ipsum'
},
'shares': [{
'uid': userId,
'date': new Date(),
'message': 'Lorem ipsum'
}]
}
before(function (done) {
mongoose.connection.on('open', done)
})
after(function (done) {
Link.remove(function () {
mongoose.connection.close(done)
})
})
it('POST /api/links', function (done) {
request.post('/api/links')
.send(sampleLink)
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.have.property('_id')
linkId = res.body._id
done()
})
})
it('GET /api/links/:id', function (done) {
request.get('/api/links/' + linkId)
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.have.property('uri', sampleLink.uri)
done()
})
})
it('GET /api/links', function (done) {
request.get('/api/links')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.have.lengthOf(1)
done()
})
})
it('PATCH /api/links/:id', function (done) {
request.patch('/api/links/' + linkId)
.send({ 'uri': 'http://2.com/' })
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.have.property('_id')
res.body.should.have.property('uri', 'http://2.com/')
done()
})
})
it('PUT /api/links/:id', function (done) {
request.put('/api/links/' + linkId)
.send(_(sampleLink).extend({ 'uri': 'http://3.com/' }))
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.have.property('_id')
res.body.should.have.property('uri', 'http://3.com/')
done()
})
})
it('DELETE /api/links', function (done) {
request.del('/api/links/' + linkId)
.expect(200)
.end(function (err, res) {
if (err) done(err)
res.body.should.be.empty
done()
})
})
})
错误:
Links resource
1) "after all" hook
✖ 1 of 6 tests failed:
1) Links resource "after all" hook:
Error: done() called multiple times
at multiple (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:177:31)
at done (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:183:26)
at NativeConnection.Runnable.run.duration (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:199:9)
at NativeConnection.EventEmitter.emit (events.js:126:20)
at open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:431:10)
at NativeConnection.Connection.onOpen (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:439:5)
at Connection._open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:399:10)
at NativeConnection.doOpen (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:48:5)
at Db.open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:230:14)
at Server.connect.connectCallback (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:333:7)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
at Server.connect.connectionPool.on.server._serverState (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
at MongoReply.parseBody (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
at Server.connect.connectionPool.on.server._serverState (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:476:22)
at EventEmitter.emit (events.js:96:17)
at _connect (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13)
at EventEmitter.emit (events.js:99:17)
at Socket.exports.Connection.createDataHandler (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:372:22)
at Socket.EventEmitter.emit (events.js:96:17)
at TCP.onread (net.js:397:14)
1) "before all" hook
✖ 1 of 6 tests failed:
1) Links resource "before all" hook:
Error: done() called multiple times
at multiple (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:177:31)
at done (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:183:26)
at NativeConnection.Runnable.run.duration (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:199:9)
at NativeConnection.EventEmitter.emit (events.js:126:20)
at open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:431:10)
at NativeConnection.Connection.onOpen (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:439:5)
at Connection._open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/connection.js:399:10)
at NativeConnection.doOpen (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:48:5)
at Db.open (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:230:14)
at Server.connect.connectCallback (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:333:7)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
at Server.connect.connectionPool.on.server._serverState (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
at MongoReply.parseBody (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
at Server.connect.connectionPool.on.server._serverState (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:476:22)
at EventEmitter.emit (events.js:96:17)
at _connect (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13)
at EventEmitter.emit (events.js:99:17)
at Socket.exports.Connection.createDataHandler (/Users/Oliver/Development/Personal/Reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:372:22)
at Socket.EventEmitter.emit (events.js:96:17)
at TCP.onread (net.js:397:14)
答案 0 :(得分:0)
我尝试用一些简单的测试用例重现这个,但我不能(mongoose 3.5.5)
这可能是修复效果,而不是原因,但可能直接删除集合会起作用吗? another SO answer的示例:
after(function (done) {
mongoose.connection.collections['links'].drop();
mongoose.connection.close(done);
});