node.js 如何使用数据在 mocha 中编写请求后测试以测试响应是否匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31176526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a post request test in mocha with data to test if response matches?
提问by Matthew Harwood
Question:How do would I write a post request test in mocha that tests if the response matches?
问题:我如何在 mocha 中编写一个 post 请求测试来测试响应是否匹配?
The response will just be a url string as it is a redirect for a 3rd party service.
响应将只是一个 url 字符串,因为它是 3rd 方服务的重定向。
Working Example Payload:
工作示例有效负载:
curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/members
member.controller.js // post method
member.controller.js // 发布方法
// Creates a new member in the DB.
exports.create = function(req, res) {
Member.findByIdAndUpdate(req.body.participant.nuid,
{ "$setOnInsert": { "_id": req.body.participant.nuid } },
{ "upsert": true },
function(err,doc) {
if (err) throw err;
res.send({
'redirectUrl': req.protocol + '://' + req.get('host') + '/registration/' + req.body.participant.nuid
})
}
);
};
Expected res.send
预期重发
{"redirectUrl":"http://localhost:9000/registration/98ASDF988SDF89SDF89989SDF9898"}
Working Example GET request Test
工作示例 GET 请求测试
var should = require('should');
var app = require('../../app');
var request = require('supertest');
describe('GET /api/members', function() {
it('should respond with JSON array', function(done) {
request(app)
.get('/api/members')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return done(err);
res.body.should.be.instanceof(Array);
done();
});
});
it('should respond with redirect on post', function(done) {
// need help here
});
});
采纳答案by javierfdezg
Try with this:
试试这个:
it('should respond with redirect on post', function(done) {
request(app)
.post('/api/members')
.send({"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) done(err);
res.body.should.have.property('participant');
res.body.participant.should.have.property('nuid', '98ASDF988SDF89SDF89989SDF9898');
});
done();
});
回答by Leonardo Venoso
You can also set the type to "form" and content type to json as I show below:
您还可以将类型设置为“表单”,将内容类型设置为 json,如下所示:
it("returns a token when user and password are valid", (done) => {
Users.createUserNotAdmin().then((user: any) => {
supertestAPI
.post("/login")
.set("Connection", "keep alive")
.set("Content-Type", "application/json")
.type("form")
.send({"email": user.email, password: "123456"})
.end((error: any, resp: any) => {
chai.expect(JSON.parse(resp.text)["token"].length).above(400, "The token length should be bigger than 400 characters.");
done();
})
});
});
You also have to set the body-parser when you create the server as I show below:
您还必须在创建服务器时设置正文解析器,如下所示:
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());

