简体   繁体   English

用jasmine-node测试

[英]testing with jasmine-node

Here's the scenario I am trying to test: 这是我试图测试的场景:

A logged in user modifies a 'module' attribute 'desc'. 登录用户修改'模块'属性'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 首先我有一个帮助登录功能://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.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. 问题是,当我从app.post执行console.log(模块)时,它显示desc现在是'test'但是我的测试失败了,因为它仍然显示默认值。

I am new to express/node and not sure how to write these sorts of tests properly. 我是表达/节点的新手,不知道如何正确编写这些类型的测试。 Any hint would be appreciated. 任何提示都将不胜感激。

PS modules : PS模块:

//Model/module.js //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;

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 我不认为你应该传递login jasmine-node提供的done回调,而是一个调用其余测试的函数

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM