[英]Simple Unit Testing NodeJS/Express
Hi I am pretty new to NodeJs and I am trying to write my first tests. 嗨,我是NodeJ的新手,我正在尝试编写我的第一个测试。
I am kind of stuck with the setup, so I was hoping for some help. 我对设置有些困惑,因此希望能提供一些帮助。
I wrote these two functions: 我写了这两个函数:
app.js:
var express = require('express')
, cors = require('cors')
, app = express();
app.get('/a_nice_jsonp',cors(corsOptions), function(req, res, next){
var result = parseCookies(req);
res.jsonp(result);
});
app.get('',function(req,res,next){
res.statusCode = 200;
res.end()
});
I do not export it as module as it is my only file. 我没有将其导出为模块,因为这是我唯一的文件。
I assume it's pretty easy to write the tests for that. 我认为为此编写测试非常容易。 I started with something like this: 我开始是这样的:
app-test.js:
var expect = require('expect.js');
var express = require('express');
var expressApp = express();
describe('app js test', function() {
describe('GET /', function() {
it('should respond to GET with empty path', function () {
expressApp.get('', function(req, res, body){
expect(res.status).to.equal(200);
});
})
});
});
I suppose it really reads like a simple task, but I seem to fail over the setup of the test and how to do it. 我想它确实看起来像是一个简单的任务,但是我似乎无法通过测试的设置和操作方法。
Can anyone help me out here? 有人可以帮我从这里出去吗?
EDIT: The above test runs fine. 编辑:上面的测试运行正常。 However, I have difficulties to test eg .end()
as well as the result
in the jsonp request
. 但是,我很难测试例如.end()
以及jsonp request
的result
。 I simple do not know how to do it?! 我简单不知道该怎么做?!
When you do 当你做
expressApp.get('', function(req, res, body){
expect(res.status).to.equal(200);
});
you are just mapping the route. 您只是在映射路线。
To test your REST API, you have to use a library like supertest (there is an example of testing using express + mocha in that link) 要测试您的REST API,您必须使用诸如supertest之类的库(在该链接中有一个使用express + mocha进行测试的示例)
it works this way 它以这种方式工作
var request = require('supertest');
var express = require('express');
var app = express();
app.get('/a_nice_jsonp',cors(corsOptions), function(req, res, next){
var result = parseCookies(req);
res.jsonp(result);
});
app.get('',function(req,res,next){
res.statusCode = 200;
res.end()
});
describe('app js test', function() {
describe('GET /', function() {
it('should respond to GET with empty path', function (done) {
request(app)
.get('')
.expect(200)
.end(done)
});
});
});
Edited with separated files 用分开的文件编辑
app.js app.js
var express = require('express')
, cors = require('cors')
, app = express();
app.get('/a_nice_jsonp',cors(corsOptions), function(req, res, next){
var result = parseCookies(req);
res.jsonp(result);
});
app.get('',function(req,res,next){
res.statusCode = 200;
res.end()
});
module.exports = app;
app-test.js app-test.js
var request = require('supertest');
var app = require('app.js');
describe('app js test', function() {
describe('GET /', function() {
it('should respond to GET with empty path', function (done) {
request(app)
.get('')
.expect(200)
.end(done)
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.