0

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;
az7ar
  • 5,187
  • 2
  • 19
  • 23

1 Answers1

2

I don't think you should be passing to login the jasmine-node supplied done callback, but rather a function where the rest of your test is called

it('should be able to change desription of a module', function(done){

    var onDoneLoggingOn = function(){
        var options = ...
        request(options, function(err, res, body){              
           ...
           done();
        });
    };
    login(onDoneLoggingOn);
});
xverges
  • 4,608
  • 1
  • 39
  • 60