【发布时间】:2022-02-10 22:21:56
【问题描述】:
我开始为我的应用程序编写一些测试,但我在读取/获取服务器响应时遇到了问题。我尝试了很多东西,但都没有真正奏效,有人可以帮帮我吗?
// /api/checkCreds
exports.checkCreds = async function(req, res){
//validation
if(!await User.checkCreds(req.body.username, req.body.password)){
var result = {error: true, data: "Incorrect"}
res.sendStatus = 401;
return res.send(JSON.stringify(result));
}
如果发送到服务器的凭据不匹配,则向用户返回带有“不正确”消息的响应。 在测试中,我试图从服务器获取数据以检查属性是否与预期输出匹配。
//test.js
it("We should fail with HTTP code 401 because incorrect data is passed (username='incorrect' password='incorrect')", function(done){
supertest(app)
.post('/api/checkCreds')
.send({username: 'incorrect', password: 'incorrect'})
.expect({error: true, data: "Incorrect"})
.expect(401, done);
});
运行时,测试失败,因为预期的属性与服务器发送的响应不同,它是一个空对象 {}。
感谢任何帮助。
【问题讨论】: