如何存根Mongoose模型构造函数

时间:2022-12-02 02:34:40

I'm trying to use Sinon.js to stub the model constructor of my Student Mongoose model.

我正在尝试使用Sinon.js来存储我的Student Mongoose模型的模型构造函数。

var Student = require('../models/student');

var student = new Student({ name: 'test student' }); // I want to stub this constructor

Looking at the Mongoose source code, Model inherits its prototype from Document, which calls the Document function, so this is what I tried in order to stub the constructor. However, my stub never gets called.

看一下Mongoose源代码,Model从Document继承了它的原型,它调用了Document函数,所以这就是我为了存根构造函数而尝试的。但是,我的存根永远不会被调用。

sinon.stub(Student.prototype__proto__, 'constructor', () => {
  console.log('This does not work!');
  return { name: 'test student' };
});

createStudent(); // Doesn't print anything

Thank you for any insight.

感谢您的任何见解。

EDIT: I can't directly set Student() as a stub, because I also stub Student.find() in another test. So my question is essentially "how do I stub Student() and Student.find() at the same time?"

编辑:我不能直接将Student()设置为存根,因为我还在另一个测试中存根Student.find()。所以我的问题基本上是“我如何同时存根Student()和Student.find()?”

1 个解决方案

#1


2  

It surely can be done only with sinon, but that will be very dependant on the way the lib works, and will not feel safe and maintainable.

它肯定只能用sinon来完成,但这将非常依赖于lib的工作方式,并且不会感到安全和可维护。

For dependancies that are hard to mock directly, you should take a look at rewire or proxyquire (I use rewire, but you may want to have a choice) to do "monkey patching".

对于难以直接模拟的依赖性,你应该看看重新连接或代理(我使用重新连接,但你可能想要选择)来做“猴子修补”。

You will use rewire like require, but it has some sugar. Example :

你将使用像需要的重新连接,但它有一些糖。示例:

var rewire = require("rewire");
var myCodeToTest = rewire("../path/to/my/code");

//Will make 'var Student' a sinon stub.
myCodeToTest.__set__('Student', sinon.stub().returns({ name: 'test'}));

//Execute code
myCodeToTest(); // or myCodeToTest.myFunction() etc..

//assert
expect...

[EDIT]

"how do I stub Student() and Student.find() at the same time?"

“我如何同时存根Student()和Student.find()?”

//Will make 'var Student' a sinon stub.
var findStub = sinon.stub().returns({});
var studentStub = sinon.stub().returns({find: findStub});
myCodeToTest.__set__('Student', studentStub);

#1


2  

It surely can be done only with sinon, but that will be very dependant on the way the lib works, and will not feel safe and maintainable.

它肯定只能用sinon来完成,但这将非常依赖于lib的工作方式,并且不会感到安全和可维护。

For dependancies that are hard to mock directly, you should take a look at rewire or proxyquire (I use rewire, but you may want to have a choice) to do "monkey patching".

对于难以直接模拟的依赖性,你应该看看重新连接或代理(我使用重新连接,但你可能想要选择)来做“猴子修补”。

You will use rewire like require, but it has some sugar. Example :

你将使用像需要的重新连接,但它有一些糖。示例:

var rewire = require("rewire");
var myCodeToTest = rewire("../path/to/my/code");

//Will make 'var Student' a sinon stub.
myCodeToTest.__set__('Student', sinon.stub().returns({ name: 'test'}));

//Execute code
myCodeToTest(); // or myCodeToTest.myFunction() etc..

//assert
expect...

[EDIT]

"how do I stub Student() and Student.find() at the same time?"

“我如何同时存根Student()和Student.find()?”

//Will make 'var Student' a sinon stub.
var findStub = sinon.stub().returns({});
var studentStub = sinon.stub().returns({find: findStub});
myCodeToTest.__set__('Student', studentStub);