最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

惩戒猫鼬模型开玩笑

运维笔记admin14浏览0评论

惩戒猫鼬模型开玩笑

惩戒猫鼬模型开玩笑

我试图嘲弄与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"

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论