【发布时间】:2016-11-18 03:20:40
【问题描述】:
如何使用 Sinon 包存根/模拟方法调用,其中我必须模拟的参数之一是使用箭头函数调用的?例如
let objWithMethod = { method : function(x) {}; };
function SUT() {
// use case
let x = 'some value';
let y = { anotherMethod : function(func) {}; };
// I want to test that `y.anotherMethod()` is called with
// `(x) => objWithMethod.method(x)` as the argument
y.anotherMethod((x) => objWithMethod.method(x));
}
let mockObj = sinon.mock(objWithMethod);
// Both of these fail with a "never called" error
mockObj.expects('method').once().withArgs(objWithMethod.method.bind(this, x));
mockObj.expects('method').once().withArgs((x) => objWithMethod.method(x));
SUT();
mockObj.verify();
我在 sinon 文档中也找不到任何东西,在谷歌搜索几次尝试后也找不到。
【问题讨论】:
-
您没有在代码中访问
objWithMethod.method。objWithMethod是一个对象,而您将其称为函数。它是如何工作的? -
您的代码导致
VM604:5 Uncaught TypeError: objWithMethod is not a function(在您修复了第一行中额外的;的问题后) -
你说得对,我的例子没有正确代表我要更新的内容
-
更新以反映我的意思
-
即使在更新之后 - 您也不会在代码中调用
objWithMethod.method。
标签: unit-testing ecmascript-6 sinon arrow-functions sinon-chai