存根对象而不需要方法
stub an object without requiring a method
我有类似的东西:
sandbox.stub(rp, 'get').resolves(successResponse)
哪个 return 是我在遇到此代码时的自定义响应:
return await rp.get(url, options)
但是我怎样才能做这样的事情:
sandbox.stub(rp).resolves(successResponse)
哪个可以 return 自定义响应,当它命中此代码时?
return await rp(url, options)
当我尝试 "stubbing" 整个对象时,我在 运行 测试时遇到此错误:
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:70:21)
at stub (node_modules\sinon\lib\sinon\stub.js:58:44)
at Object.stub (node_modules\sinon\lib\sinon\collection.js:93:33)
rp
是 request-promise-native, which wraps request
来自@Troopers' link 在上面的评论中,这似乎是不可能的:Doing this is not technically possible without faking the entire module loading system.
所以我遵循了这里的建议:https://github.com/request/request/issues/2181 并使用 mock-require 存根 rp
。我还更改了用于调用 rp.get()
的代码,以便它只调用 rp()
,因为我也不知道如何存根 rp()
和 rp.get()
您可能会发现有关 Link Seams 的 Sinon HowTo 有帮助:http://sinonjs.org/how-to/link-seams-commonjs/
我有类似的东西:
sandbox.stub(rp, 'get').resolves(successResponse)
哪个 return 是我在遇到此代码时的自定义响应:
return await rp.get(url, options)
但是我怎样才能做这样的事情:
sandbox.stub(rp).resolves(successResponse)
哪个可以 return 自定义响应,当它命中此代码时?
return await rp(url, options)
当我尝试 "stubbing" 整个对象时,我在 运行 测试时遇到此错误:
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:70:21)
at stub (node_modules\sinon\lib\sinon\stub.js:58:44)
at Object.stub (node_modules\sinon\lib\sinon\collection.js:93:33)
rp
是 request-promise-native, which wraps request
来自@Troopers' link 在上面的评论中,这似乎是不可能的:Doing this is not technically possible without faking the entire module loading system.
所以我遵循了这里的建议:https://github.com/request/request/issues/2181 并使用 mock-require 存根 rp
。我还更改了用于调用 rp.get()
的代码,以便它只调用 rp()
,因为我也不知道如何存根 rp()
和 rp.get()
您可能会发现有关 Link Seams 的 Sinon HowTo 有帮助:http://sinonjs.org/how-to/link-seams-commonjs/