不同的参数/返回值为sinon期望

我正在为我的模块编写unit testing,并使用SinonJS来validation对其他模块的函数调用的期望。 首先,我为其他模块注册一个模拟:

var otherModule = { getConfig : function () {} }; mockery.registerMock("otherModule", otherModule); 

之后,我运行一些testing,并(成功)validation一些期望,如:

 var otherModuleMock = sinon.mock(otherModule); otherModuleMock .expects("getConfig") .once() .withArgs("A") .returns(configValuesForA); // run test otherModuleMock.verify(); // <- succeeds 

我碰到一个问题,但是,当模块调用getConfig函数两次,不同的参数:

 otherModuleMock .expects("getConfig") .once() .withArgs("A") .returns(configValuesForA); otherModuleMock .expects("getConfig") .once() .withArgs("B") .returns(configValuesForB); 

从我对文档的理解,这应该可能工作。 但是,这会导致以下错误:

 ExpectationError: Unexpected call: getConfig(A) Expectation met: getConfig(A[, ...]) once Expectation met: getConfig(B[, ...]) once 

我尝试用atLeast(1)replaceonce() atLeast(1)或完全删除它。 我也尝试捕获otherModuleMock.expect("getConfig")返回的期望值,并应用withArgsreturns 。 两者都无济于事。

很确定我用我不应该的方式使用mock() ,但我应该怎么走?

发现我正在testing的模块实际上调用了getConfig 3次:一次使用A ,一次使用B ,然后再使用A

显然,Sinon跟踪所有期望/调用的顺序,这就是使用atLeast(1)而不是once()的testing失败的原因。 当我现在看它时是有道理的,但是这些案例在Sinon文档(我自己的辩护)中并没有被广泛记载。