【发布时间】:2016-05-07 17:28:56
【问题描述】:
是否可以让 sinon 窥探函数表达式?以这段代码为例。
function one() { return 1; }
function two() { return 2; }
function three() { return 3; }
function myMethod() {
var n1 = one();
var n2 = two();
var n3 = three();
return n1 + n2 + n3;
}
QUnit.module('My test');
QUnit.test('testing functions', (assert) => {
assert.expect(3);
const spyOne = sinon.spy(one);
const spyTwo = sinon.spy(two);
const spyThree = sinon.spy(three);
myMethod();
assert.ok(spyOne.called, "called one");
assert.ok(spyTwo.called, "called two");
assert.ok(spyThree.called, "called three");
sinon.restore();
});
即使我打电话给myMethod() 并且我在one - two - three 上有间谍,我仍然在one.called 上得到错误消息(two 和three 也是如此)
我在这里错过了什么?
谢谢!
【问题讨论】:
标签: javascript unit-testing sinon function-expression