相关疑难解决方法(0)

一个单元如何用Express测试路线?

我正在学习Node.js并且一直在使用Express.真的很喜欢框架;但是,我无法弄清楚如何为路线编写单元/集成测试.

能够对简单模块进行单元测试很容易,并且已经使用Mocha进行了测试 ; 但是,我的单元测试使用Express失败,因为我传入的响应对象不保留值.

正在测试的路由功能(routes/index.js):

exports.index = function(req, res){
  res.render('index', { title: 'Express' })
};
Run Code Online (Sandbox Code Playgroud)

单元测试模块:

var should = require("should")
    , routes = require("../routes");

var request = {};
var response = {
    viewName: ""
    , data : {}
    , render: function(view, viewData) {
        viewName = view;
        data = viewData;
    }
};

describe("Routing", function(){
    describe("Default Route", function(){
        it("should provide the a title and the index view name", function(){
        routes.index(request, response);
        response.viewName.should.equal("index");
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它失败了"错误:检测到全局泄漏:viewName,data".

  1. 我哪里出错,以便我可以让这个工作?

  2. 有没有更好的方法让我在这个级别对我的代码进行单元测试?

更新 …

node.js express

90
推荐指数
5
解决办法
6万
查看次数

标签 统计

express ×1

node.js ×1