Here's the scenario I am trying to test:
A logged in user modifies a 'module' attribute 'desc'. I am not using any database for storing 'module', it is defined in a file that I am requiring.
Here's the test code that is not working:
first I have a helper login function : //app.spec.js
var login = function(done) {
var options = {
uri: 'http://localhost:3000/login'
, method: 'POST'
, form: {
username: 'user'
, password: 'ffff'
}
}
request(options, function() {
done();
});
};
and the test :
it('should be able to change desription of a module', function(done){
login(done);
var options = {
uri: 'http://localhost:3000/module/1'
, method: 'POST'
, form: {
desc: 'test'
}
}
request(options, function(err, res, body){
var modules = require('../Model/module').modules;
console.log(modules);
expect(modules[0].desc).toBe('test');
done();
});
});
Finally,
//app.js
app.post('/module/:module_id', ensureAuthenticated, function(req, res) {
var desc = req.body['desc'];
if(req.module_id){
findModuleById(req.module_id, function(err, module) {
module.desc = desc;
console.log('changed module');
console.log(module);
});
}
res.redirect('/');
});
The problem is when I am doing console.log(modules) from app.post it is showing the desc is now 'test' but my test fails because there it still shows the default value.
I am new to express/node and not sure how to write these sorts of tests properly. Any hint would be appreciated.
P.S. modules :
//Model/module.js
var modules = [
{id: 1, desc: 'Default description for module 1'}
, {id: 2, desc: 'Default description for module 2'}
, {id: 3, desc: 'Default description for module 3'}
];
module.exports.modules = modules;