1

--watch最初の実行では問題なく合格する Mocha テストがありますが、変更が行われ、Mocha が (フラグを使用して) 2 回目のテストを再実行すると、失敗します。私のテスト:

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)
4

1 に答える 1

0

いくつかの簡単なテストケースでこれを再現しようとしましたが、できませんでした (mongoose 3.5.5)

これはおそらく原因ではなく効果を修正していますが、コレクションを直接削除するとうまくいくでしょうか? 別の SO 回答の例:

  after(function (done) {
    mongoose.connection.collections['links'].drop();
    mongoose.connection.close(done);
  });
于 2013-02-24T12:27:28.617 に答える