gpt4 book ai didi

node.js - 如何使用数据在 mocha 中编写发布请求测试以测试响应是否匹配?

转载 作者:IT老高 更新时间:2023-10-28 22:12:10 34 4
gpt4 key购买 nike

问题:我如何在 mocha 中编写一个发布请求测试来测试响应是否匹配?

响应将只是一个 url 字符串,因为它是 3rd 方服务的重定向。

工作示例负载:

curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/members

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
})
}
);
};

预期 res.send

 {"redirectUrl":"http://localhost:9000/registration/98ASDF988SDF89SDF89989SDF9898"}  

工作示例 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
});
});

最佳答案

试试这个:

  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();
});

关于node.js - 如何使用数据在 mocha 中编写发布请求测试以测试响应是否匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31176526/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com