我正在使用chai和mocha为我的api运行测试用例,chai http .I返回了500的响应,即使我的测试用例也通过了。
这是我的测试用例
describe('/POST saveBatch', () => {
it('it should save the Batch', (done) => {
chai.request(app)
.post('/batches/saveBatch')
.set('content-type', 'application/x-www-form-urlencoded')
.send({batch_id: 1})
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
});
这是我的控制器
batchController.saveBatch=async(req,res)=>{
let _value=await batchService.saveBatch(req.body);
if(_value.error)
{
//throw new Error(_value.data);
res.json({data:"Error Occurred"}).status(500);
}
else{
res.json({data:"Success"}).status(200);
}
}
我返回500状态,但它仍然显示我的测试用例将通过。谢谢你的帮助
发布于 2020-01-31 16:10:22
您是否尝试使用console.log来查看从batchService返回的"_value“的实际值?因为我非常确定您的控制器函数实际上不会进入您的if(_value.error)
,否则它将在expect(res).to.have.status(200);
上失败
https://stackoverflow.com/questions/60001263
复制