惩戒猫鼬模型开玩笑
我试图嘲弄与jest
猫鼬的模式,但越来越Cannot create property 'constructor' on number '1'
错误。我能够通过创建2个文件如下图所示的项目重现该问题。有没有一种方法来嘲笑与jest
猫鼬模式?
./model.就是
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const schema = new Schema({
name: String
})
module.exports = mongoose.model('Test', schema)
./model.test.就是
jest.mock('./model')
const Test = require('./model')
// Test.findOne.mockImplementation = () => {
// ...
// }
错误:
FAIL ./model.test.js
● Test suite failed to run
TypeError: Cannot create property 'constructor' on number '1'
at ModuleMockerClass._generateMock (../../jitta/sandbox/rest_api/node_modules/jest-mock/build/index.js:458:34)
at Array.forEach (native)
at Array.forEach (native)
at Array.forEach (native)
更新:
似乎是在开玩笑的错误。
回答如下:另一解决方案是spyOn
模型prototype
功能。
例如,这将使MyModel.save()
失败:
jest.spyOn(MyModel.prototype, 'save')
.mockImplementationOnce(() => Promise.reject('fail update'))
您可以使用mockImplementationOnce
到不必mockRestore
间谍。但你也可以使用mockImplementation
和使用这样的:
afterEach(() => {
jest.restoreAllMocks()
})
测试了"mongoose": "^4.11.7"
和"jest": "^23.6.0"
。