用开玩笑模仿猫鼬模型

时间:2017-03-06 07:35:11

标签: node.js mongodb jestjs

我正在尝试使用jest模拟mongoose模型,但是出现Cannot create property 'constructor' on number '1'错误。我能够通过使用下面显示的2个文件创建项目来重现该问题。有没有办法用jest来模拟猫鼬模型?

./ model.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const schema = new Schema({
  name: String
})

module.exports = mongoose.model('Test', schema)

./ model.test.js

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)

更新

似乎是开玩笑的错误。 https://github.com/facebook/jest/issues/3073

4 个答案:

答案 0 :(得分:8)

好吧,我有同样的问题,所以我创作这个包来解决这个问题: https://www.npmjs.com/package/mockingoose

这就是你如何使用它,让我们说这是你的模型:

import mongoose from 'mongoose';
const { Schema } = mongoose;

const schema = Schema({
    name: String,
    email: String,
    created: { type: Date, default: Date.now }
})

export default mongoose.model('User', schema);

这是你的测试:

it('should find', () => {
  mockingoose.User.toReturn({ name: 2 });

  return User
  .find()
  .where('name')
  .in([1])
  .then(result => {
    expect(result).toEqual({ name: 2 });
  })
});

签出tests文件夹以获取更多示例: https://github.com/alonronin/mockingoose/blob/master/tests/index.test.js

没有与数据库建立联系!

答案 1 :(得分:5)

另一种解决方案是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"测试。

答案 2 :(得分:0)

模拟鹅似乎是一个非常好的解决方案。但是我也可以使用Jest.mock()函数来模拟我的模型。至少使用create方法。

// in the module under the test I am creating (saving) DeviceLocation to DB
// someBackendModule.js
...
DeviceLocation.create(location, (err) => {
...
});
...

DeviceLocation模型定义:

// DeviceLocation.js
import mongoose from 'mongoose';

const { Schema } = mongoose;
const modelName = 'deviceLocation';

const DeviceLocation = new Schema({
...
});

export default mongoose.model(modelName, DeviceLocation, modelName);
在与__mocks__模型相同的文件夹中的DeviceLocation文件夹中的

DeviceLocation模拟:

// __mock__/DeviceLocation.js
export default {
    create: (x) => {    
        return x;
    },
};

在测试文件中:

// test.js
// calling the mock
...
jest.mock('../../src/models/mongoose/DeviceLocation');
import someBackendModule from 'someBackendModule';
...
// perform the test

答案 3 :(得分:0)

对于打字稿,我发现了一个可行的黑客

UPDATE r2 SET r2.Role1 = @role1 FROM #roles AS r1 JOIN #roles AS r2 ON r1.Role2 = r2.Role2 WHERE r1.Role1 = @role1 AND r2.Role1 <> @role1; SELECT * FROM #roles; +----+-------+-------+-----+ | ID | Role1 | Role2 | NOT | +----+-------+-------+-----+ | 1 | 133 | AB | 0 | | 2 | 133 | AB | 1 | | 3 | 789 | EF | 0 | | 4 | 133 | AB | 0 | | 5 | 453 | EF | 1 | | 6 | 764 | DF | 0 | +----+-------+-------+-----+