Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Mocha Unit Testing error re: Mongoose mocks with Mockgoose, "Error setting TTL index on collection : sessions"

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

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.

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.

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

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.

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

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
like image 205
Charney Kaye Avatar asked Mar 22 '14 19:03

Charney Kaye


3 Answers

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

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.

The problem is that this "sessions" collection is being specified directly in the Express framework configuration, not implementing 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!

like image 114
Charney Kaye Avatar answered Nov 02 '22 00:11

Charney Kaye


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.

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

like image 2
Anthony McCormick Avatar answered Nov 02 '22 02:11

Anthony McCormick


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:

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
        })
    }));
}
like image 1
Matt Avatar answered Nov 02 '22 00:11

Matt