Mocha/Chai Nodejs/Express API 的简单测试
Simple Mocha/Chai test for Nodejs/Express API
我正在尝试为我的 API 构建一个测试集,它是用 nodejs/express 和 mocha/chai 开发的。基本上,索引 returns 是一个简单的字符串,我知道它在工作,因为我可以在我的浏览器上看到:
router.get('/', function(req, res, next) {
res.send('Hello World from Eclipse');
});
然后我按照这个 tutorial 构建了这个测试:
var supertest = require("supertest");
var should = require("should");
// This agent refers to PORT where program is runninng.
var server = supertest.agent("http://localhost:5000");
// UNIT test begin
describe("SAMPLE unit test",function(){
// #1 should return home page
it("should return home page",function(done){
// calling home page api
server
.get("/")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// HTTP status should be 200
res.status.should.equal(200);
// Error key should be false.
res.body.error.should.equal(false);
done();
});
});
});
我可以在我的服务器日志中看到该地址已被调用,但是,测试总是说它无法读取未定义的 属性 'should',在我所在的行'res.status.should.equal(200);'。可能是因为 'res' 未定义。换句话说,API.
没有答案
我是不是漏掉了什么?我 运行 没有参数的 mocha...
尝试这样的事情:
var expect = require('chai').expect;
var request = require('supertest');
var uri = 'your url';
describe('Profile',function(){
it('Should return a users profile',function(done){
request
.get(uri + '/api/1.0/profile')
.query({app_id:'12345'})
.end(function(err,res){
var body = res.body;
expect(body.first_name).to.equal('Bob');
expect(body.last_name).to.equal('Smith');
done()
});
});
});
确保包含正确的要求。
您应该检查 .end()
中的错误:
.end(function(err, res) {
if (err) return done(err);
...
});
测试用例期望内容类型匹配 /json/
,但事实并非如此,因此应该设置它(因此 res
将是未定义的)。
我正在尝试为我的 API 构建一个测试集,它是用 nodejs/express 和 mocha/chai 开发的。基本上,索引 returns 是一个简单的字符串,我知道它在工作,因为我可以在我的浏览器上看到:
router.get('/', function(req, res, next) {
res.send('Hello World from Eclipse');
});
然后我按照这个 tutorial 构建了这个测试:
var supertest = require("supertest");
var should = require("should");
// This agent refers to PORT where program is runninng.
var server = supertest.agent("http://localhost:5000");
// UNIT test begin
describe("SAMPLE unit test",function(){
// #1 should return home page
it("should return home page",function(done){
// calling home page api
server
.get("/")
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// HTTP status should be 200
res.status.should.equal(200);
// Error key should be false.
res.body.error.should.equal(false);
done();
});
});
});
我可以在我的服务器日志中看到该地址已被调用,但是,测试总是说它无法读取未定义的 属性 'should',在我所在的行'res.status.should.equal(200);'。可能是因为 'res' 未定义。换句话说,API.
没有答案我是不是漏掉了什么?我 运行 没有参数的 mocha...
尝试这样的事情:
var expect = require('chai').expect;
var request = require('supertest');
var uri = 'your url';
describe('Profile',function(){
it('Should return a users profile',function(done){
request
.get(uri + '/api/1.0/profile')
.query({app_id:'12345'})
.end(function(err,res){
var body = res.body;
expect(body.first_name).to.equal('Bob');
expect(body.last_name).to.equal('Smith');
done()
});
});
});
确保包含正确的要求。
您应该检查 .end()
中的错误:
.end(function(err, res) {
if (err) return done(err);
...
});
测试用例期望内容类型匹配 /json/
,但事实并非如此,因此应该设置它(因此 res
将是未定义的)。