Node.js 参数don';t通过柴
我正在尝试测试通过chai的PUT路径。我通过chai发送的数据没有正确地传递到函数中。当我使用console.log检查loanID的值时,它是未定义的。我不确定这是因为我使用了一种不正确的方法来传递方法。当使用邮递员进行测试时,该路线运行良好 摩卡柴测试:Node.js 参数don';t通过柴,node.js,mocha.js,chai,Node.js,Mocha.js,Chai,我正在尝试测试通过chai的PUT路径。我通过chai发送的数据没有正确地传递到函数中。当我使用console.log检查loanID的值时,它是未定义的。我不确定这是因为我使用了一种不正确的方法来传递方法。当使用邮递员进行测试时,该路线运行良好 摩卡柴测试: describe('PUT /loanApproval', () => { it('it should give approval to loans', (done) => { const loan =
describe('PUT /loanApproval', () => {
it('it should give approval to loans', (done) => {
const loan = new Loan({ loanID : 1,
manager : 'John Doe',
status : 'Approve'});
loan.save((err, loan) => {
chai.request(server)
.put('/loanApproval')
.send(loan)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('result');
res.body.result.should.have.property('loanID').eql(1);
res.body.result.should.have.property('manager');
res.body.result.should.have.property('status');
done();
});
});
});
});
功能:
//loan aproval
this.loanApproval = function(req,res) {
const loanID = req.params.loanID;
//geting existing details from loan
Loan.findOne({ 'loanID' : loanID }, function(err, loan) {
if(err) {
console.log(err);
return res.send({'error':err});
}
else if (!loan) {
// If loan doesn't exist i.e. the wrong loanID was given
req.log.error('Loan does not exist to update: ', loanID);
return res.json({'error':'Record does not exist'});
}
//update deteails
loan.manager = req.params.manager;
loan.status = req.params.status;
//send data to database
loan.save(function(err, result) {
if(err) {
console.log(err);
return res.send({'error':err});
}
else {
return res.json({'Aproval details':result});
}
});
});
};
错误:
Loan PUT /loanApproval it should give approval to loans:
Uncaught AssertionError: expected { error: 'Record does not exist' } to
have a property 'result'
at chai.request.put.send.end (test\loan.js:261:46)
at Test.Request.callback (node_modules\superagent\lib\node\index.js:619:12)
at node_modules\superagent\lib\node\index.js:795:18
at IncomingMessage.<anonymous>
(node_modules\superagent\lib\node\parsers\json.js:16:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
贷款出售/贷款批准应批准贷款:
未捕获的断言错误:应为{错误:'记录不存在'}
具有属性“结果”
at chai.request.put.send.end(test\loan.js:261:46)
在Test.Request.callback(node\u modules\superagent\lib\node\index.js:619:12)
在node\u modules\superagent\lib\node\index.js:795:18
在收到消息时。
(node\u modules\superagent\lib\node\parsers\json.js:16:7)
在endReadableNT(_stream_readable.js:975:12)
at _combinedTickCallback(内部/流程/下一步_tick.js:80:11)
在进程中。_tickDomainCallback(internal/process/next_tick.js:128:9)
您的路由处理程序函数正在使用,该函数用于命名路由参数(使用:xxx
占位符声明的路由)
您的测试建议您不要使用req.params
,而应该使用req.body
我不知道为什么它会像你说的那样在邮递员身上起作用