简体   繁体   English

nodejs / express测试

[英]nodejs/express testing

I'm writing an app with express. 我正在用快递写一个应用程序。

The main file is called server.js 主文件名为server.js

one of the objects is 其中一个对象是

var app = express.createServer(express.logger(),
    express.bodyDecoder());

then I have 然后我有

app.get("/",function(req, res){
  res.send("Running");
}

How do I use expresso, [or any other testing framework for that matter], to test the routes output? 如何使用expresso [或任何其他测试框架]来测试路由输出?

I looked at the expresso site, but couldn't figure out how to get it to all work together, is it possible if someone gave me a short example? 我看了一下expresso网站,但无法弄清楚如何让它一起工作,如果有人给我一个简短的例子,是否可能?

Thanks! 谢谢!

I'm sure you have found the answer to this by now but have you tried something like this: 我相信你现在已经找到了答案,但是你尝试过这样的事情:

assert.response(server, {
    url: '/',
    method: 'GET'
},{
    body: '{"name":"tj"}',
    status: 200,
    headers: {
        'Content-Type': 'application/json; charset=utf8',
        'X-Foo': 'bar'
    }
});

If you're new to Node.js and getting confused with all the different testing libs: 如果您是Node.js的新手,并且对所有不同的测试库感到困惑:

Expresso's successor is Mocha . Expresso的继任者是摩卡 In Mocha, you use a separate tool for HTTP response testing, such as Request , SuperTest , zombie.js , etc. 在Mocha中,您使用单独的工具进行HTTP响应测试,例如RequestSuperTestzombie.js等。

Zombie.js seems the easiest to learn how to use, esp. Zombie.js似乎最容易学习如何使用,尤其是。 w/ cookies and cookie-based sessions. w / cookies和基于cookie的会话。 It "maintains state across requests: history, cookies, HTML5 local and session stroage, etc." 它“跨越请求维护状态:历史,cookie,HTML5本地和会话连接等”。

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

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