As part of setting up our continuous integration cycle we are defining standard templates for unit and integration test for our mean applications. One basic test is to verify some settings in a mongo collection (name Setting) We are using Mocha, Should, Require and super test
I've defined the following test
var request = require('supertest'); describe('configuration',function(){
before(function(done) { // In our tests we use the test db mongoose.connect(config.db); done(); }); describe('required configuration ',function(){ it('should return Settings Object',function(done){ mongoose.Setting.findOne({key:'ABC'}, function(foundSetting){ foundSetting.value.should.equal('XYZ'); done(); }); }) });
})
which always errors with 'TypeError: Cannot call method 'findOne' of undefined'
can anyone point me in the right direction how to use integration test on mongo objects (also e.g. checking a collection count
Considering Setting is a mongoose model you would have this at the end of the model file
setting.js
...
module.exports = mongoose.model('Setting', settingSchema)
And I think if you require this model in your test file like this
test.js
var Setting = require('./setting')
Then you could call Setting.findOne
instead of mongoose.Setting.findOne
inside the actual test case and it should work fine. Also you should make sure you have successfully connected to your test database before you do this. It is a good practice to clean the tested collection in a before hook
before(function(done) {
// Connect to database here
Setting.remove({}, function(err) {
if (err)
throw err
else
done()
})
})
and do the same thing afterEach
test case.
I don't see where and how you use supertest in your snippet but I believe it is the right tool. Again, in your before hook you could first call request = request(require('./app'))
where app.js exports your express application. Moreover, you could move the code that connects to the database to your app.js, which feels more environment-independent.