Wednesday, 31 May 2017

Parameter don't pass in chai

I'm try to test my PUT route through chai put the data I send through chai doesn't get passed properly into the function. When I used console.log to check the value of loanID it comes as undefined. I'm not sure its because I'm using an incorrect way to pass the methods. The route works fine when tested using Postman.

Mocha-Chai test:

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

Function:

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



via BattleFrog

No comments:

Post a Comment