Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Sinon存根mongoose模型_Node.js_Mongoose_Tdd_Sinon - Fatal编程技术网

Node.js 使用Sinon存根mongoose模型

Node.js 使用Sinon存根mongoose模型,node.js,mongoose,tdd,sinon,Node.js,Mongoose,Tdd,Sinon,我试图存根一个mongoose模型以返回一个json值 我的密码是 var valueToReturn = { name:'xxxxx' }; var stub = sinon.stub(MyModel.prototype,'findOne'); stub.returns(valueToReturn); 我得到这个错误:TypeError:试图将未定义的属性findOne包装为函数请查看。您可以使用仅几行

我试图存根一个mongoose模型以返回一个json值

我的密码是

var valueToReturn = {
                      name:'xxxxx'
                     };

var stub = sinon.stub(MyModel.prototype,'findOne');

stub.returns(valueToReturn);
我得到这个错误:TypeError:试图将未定义的属性findOne包装为函数

请查看。您可以使用仅几行的链式方法:

// If you are using callbacks, use yields so your callback will be called
sinon.mock(YourModel)
  .expects('findById').withArgs('abc123')
  .chain('exec')
  .yields(someError, someResult);

// If you are using Promises, use 'resolves' (using sinon-as-promised npm) 
sinon.mock(YourModel)
  .expects('findById').withArgs('abc123')
  .chain('exec')
  .resolves(someResult);
您可以找到回购协议的工作示例


另外,还有一个建议:使用
mock
方法而不是
stub
,这将检查原始对象上是否存在该方法。

看看这里,他们使用的是mongoose.Model的findOne方法。我正在尝试使用MyModel.findOne,因为我的导出方法findOne用于两个不同的模型。所以我想尝试两种不同的模式你是sinon mongoose的创造者吗?您可以在7-8个不同的mongoose测试线程中插入它。@VtoCorleone是的,我共享了它,因为我认为它可能对其他面临与我在模拟mongoose模型时遇到的相同问题的人有用:)