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

时间:2021-03-25 02:33:41

Making this open source application, outrightmental/partybot-web.git on GitHub built on Daftmonk's mighty Yeoman angular-fullstack generator.

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

I've been trying to wrap my skills around the seemingly hot contemporary problem of doing MongoDB mocks during testing (local and CI) of Node.js.

我一直试图将我的技能包含在Node.js测试(本地和CI)期间看起来很热的当代问题上做MongoDB模拟。

I'm a fan of Mocha for running and Chai for assertion. After knocking out many early contenders, I'm experimenting now with Mockgoose for mocking behind Mongoose models in lieu of MongoDB.

我是摩卡跑步和Chai断言的粉丝。在淘汰了许多早期的竞争者之后,我现在正在尝试使用Mockgoose来嘲笑Mongoose模型以代替MongoDB。

See also the build pertaining to this issue, in my open source partybot-web CI on Travis

另请参阅我在Travis上的开源partybot-web CI中与此问题相关的构建

So far, this configuration has been able to successfully run some database operations, proving that Mockgoose is not a complete waste of time. However, this "sessions" table seems to be stumping it.

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

Please see example local execution below (Ubuntu 12), and let me know if any Node wizards out there have a pearl of wisdom...

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

Thanks very much! -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 个解决方案

#1


4  

The "sessions" MongoDB collection is being requested here, by the Node.js Express framework's "sessions" module:

这里通过Node.js Express框架的“会话”模块请求“会话”MongoDB集合:

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

I have been using Mockgoose to mock my Mongoose-wrapped MongoDB tables.. and it is doing its job perfectly.

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

The problem is that this "sessions" collection is being specified directly in the Express framework configuration, not implementing Mongoose:

问题是这个“sessions”集合是直接在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");
  })
}));

I'll have to do my Express session mocking separately!

我必须单独做我的Express会话嘲笑!

#2


2  

Not all of Mongoose is fully implemented yet. However if you can add a test case that shows this issue then I normally will focus on implementing that item over the others on my list.

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

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

#3


1  

I just ran into the same issue - my solution was to use express-session's memorystore option (the default when no other is specified), rather than mongodb. Really quickly, I just did this:

我刚遇到同样的问题 - 我的解决方案是使用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
        })
    }));
}

#1


4  

The "sessions" MongoDB collection is being requested here, by the Node.js Express framework's "sessions" module:

这里通过Node.js Express框架的“会话”模块请求“会话”MongoDB集合:

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

I have been using Mockgoose to mock my Mongoose-wrapped MongoDB tables.. and it is doing its job perfectly.

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

The problem is that this "sessions" collection is being specified directly in the Express framework configuration, not implementing Mongoose:

问题是这个“sessions”集合是直接在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");
  })
}));

I'll have to do my Express session mocking separately!

我必须单独做我的Express会话嘲笑!

#2


2  

Not all of Mongoose is fully implemented yet. However if you can add a test case that shows this issue then I normally will focus on implementing that item over the others on my list.

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

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

#3


1  

I just ran into the same issue - my solution was to use express-session's memorystore option (the default when no other is specified), rather than mongodb. Really quickly, I just did this:

我刚遇到同样的问题 - 我的解决方案是使用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
        })
    }));
}