尝试将未定义的属性findOneAndUpdate包装为函数

时间:2019-04-07 15:34:11

标签: javascript node.js mocha sinon chai

我使用PUT /cars/:idSinon.js编写了单元测试: unitTest.js:

const sinon = require('sinon');
const chai = require('chai');
const expect = chai.expect;
const mongoose = require('mongoose');
// Monck mongodb Models
require('sinon-mongoose');

const Car = require('../../models/Car');
describe('PUT /cars/:id', () => {
        it('Should update a car successfully', (done) => {
            const updateBody = {
                name: 'new car name',
            }
            const CarMock = sinon.mock(new Car(updateBody));
            const car = CarMock.object;
            const expectedResult = {    
                statusCode: 200
            }
            const id = mongoose.Types.ObjectId();
            CarMock.expects('findOneAndUpdate').withArgs(id).yields(null, expectedResult);
            car.findOneAndUpdate(id, updateBody, (error, res) => {
                if (error) done(error)
                CarMock.verify();
                CarMock.restore();
                expect(res.statusCode).to.equal(200);
                done();
            })
        })
})

运行测试时出现此错误:

     PUT /cars/:id
       Should update a car successfully:
 TypeError: Attempted to wrap undefined property findOneAndUpdate as function
  at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:72:21)
  at Object.expects (node_modules\sinon\lib\sinon\mock.js:71:13)
  at Object.mock.expects (node_modules\sinon-mongoose\dist\index.js:49:37)
  at Context.it (server\test\cars\unitTests.js:106:29)

我编写此测试时犯了什么错误?

1 个答案:

答案 0 :(得分:0)

为通过测试,我将代码更新为:

fshrc