Node.js Mocha单元测试错误re:使用Mockgoose的Mongoose模拟,"设置收集时的TTL索引时出错:会话"

时间:2014-03-22 19:45:28

标签: node.js mongodb unit-testing mocking mongoose

在Daftmonk强大的Yeoman outrightmental/partybot-web.git on GitHub 生成器上构建这个开源应用程序 angular-fullstack

我一直在努力将我的技能包含在Node.js测试(本地和CI)期间看似热门的当代问题中进行MongoDB模拟。

我喜欢 Mocha 用于运行, Chai 用于断言。淘汰了许多早期的竞争者之后,我现在正在尝试使用 Mockgoose 来模仿Mongoose模型而不是MongoDB。

另请参阅我的开源 partybot-web CI on Travis

中与此问题相关的构建

到目前为止,这个配置已经能够成功运行一些数据库操作,证明Mockgoose不是完全浪费时间。但是,这个“会话”表似乎很难过。

请参阅下面的示例本地执行(Ubuntu 12),让我知道是否有任何Node向导有一颗智慧之珠......

非常感谢! -Nick

nick@om-ultrabook-ubuntu:~/Development/partybot-web$ mocha test/server/api/incoming/sms.js 
  info  - socket.io started
    Express server listening on port 3000 in development mode
Error: Error setting TTL index on collection : sessions
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:161:23
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1404:28
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1542:30
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:159:22
  at commandHandler (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:48)
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1806:9
  at __executeQueryCommand (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1636:44)
  at Db._executeQueryCommand (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1805:7)
  at Cursor.nextObject (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:729:13)
  at Cursor.toArray (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:158:10)
  at Cursor.toArray (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/scope.js:10:20)
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1541:65
  at Db.collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:495:44)
  at Db.indexInformation (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1539:8)
  at Db.ensureIndex (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1403:8)
  at Collection.ensureIndex (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/collection/index.js:65:11)
  at /home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:159:29
  at Db.collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:495:44)
  at MongoStore._get_collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:150:17)
  at MongoStore.set (/home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:262:12)
  at Session.save (/home/nick/Development/partybot-web/node_modules/express/node_modules/connect/node_modules/express-session/session/session.js:63:25)
  at ServerResponse.res.end (/home/nick/Development/partybot-web/node_modules/express/node_modules/connect/node_modules/express-session/index.js:304:19)
  at twilioResponse (/home/nick/Development/partybot-web/lib/controllers/incoming.js:19:7)
  at Promise.<anonymous> (/home/nick/Development/partybot-web/lib/controllers/incoming.js:58:5)
  at Promise.<anonymous> (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
  at Promise.EventEmitter.emit (events.js:98:17)
  at Promise.emit (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
  at Promise.fulfill (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
  at handleSave (/home/nick/Development/partybot-web/node_modules/mongoose/lib/model.js:133:13)
  at /home/nick/Development/partybot-web/node_modules/mongoose/lib/utils.js:408:16
  at /home/nick/Development/partybot-web/node_modules/mockgoose/lib/Collection.js:116:13
  at Object.validateOptions [as validate] (/home/nick/Development/partybot-web/node_modules/mockgoose/lib/validation/Validation.js:40:5)
  at Collection.insert (/home/nick/Development/partybot-web/node_modules/mockgoose/lib/Collection.js:110:20)
  at model.save (/home/nick/Development/partybot-web/node_modules/mongoose/lib/model.js:190:21)
  at model._done (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:59:24)
  at _next (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:52:28)
  at fnWrapper (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:159:8)
  at model.<anonymous> (/home/nick/Development/partybot-web/lib/models/message.js:31:3)
  at _next (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:50:30)
  at fnWrapper (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:159:8)
  at complete (/home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:974:5)
  at /home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:965:20
  at ObjectId.SchemaType.doValidate (/home/nick/Development/partybot-web/node_modules/mongoose/lib/schematype.js:603:22)
  at /home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:956:9
  at process._tickCallback (node.js:415:13)
GET /api/incoming/sms 200 21ms
․db connection open
POST /api/incoming/sms 200 6ms

3 个答案:

答案 0 :(得分:4)

Node.js Express框架的“会话”模块正在请求“sessions”MongoDB集合:

Session.save (node_modules/express-session/session/session.js:63:25)

我一直在使用Mockgoose来模拟我的Mongoose包裹的MongoDB表.. 它完美地完成了它的工作

问题是这个“会话”集合是直接在Express框架配置中指定的,没有实现Mongoose

    // Persist sessions with mongoStore
app.use(express.session({
  secret: 'angular-fullstack secret',
  store: new mongoStore({
    url: config.mongo.uri,
    collection: 'sessions'
  }, function () {
      console.log("db connection open");
  })
}));

我必须单独进行Express会话模拟!

答案 1 :(得分:2)

并非所有Mongoose都已完全实现。但是,如果您可以添加一个显示此问题的测试用例,那么我通常会专注于在列表中的其他项目上实现该项目。

https://github.com/mccormicka/Mockgoose/issues/38

答案 2 :(得分:1)

我刚遇到同样的问题 - 我的解决方案是使用express-session的memorystore选项(默认情况下没有指定其他选项),而不是mongodb。真的很快,我刚才这样做了:

if (process.env.NODE_ENV === 'test') {
    app.use(session({ secret: config.sessionSecret }));
} else {
    app.use(session({
        secret: config.sessionSecret,
        store: new mongoStore({
            //db: db.connection.db,
            mongoose_connection: db.connection,
            collection: config.sessionCollection
        })
    }));
}