我正在尝试使用 OCMockito 来 stub NSJSONSerialization
方法。我以为我有一个解决方案,但事实证明它会导致此异常:
*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
这就是我正在做的事情:
Class mockClass = MKTMockClass([NSJSONSerialization class]);
MKTOngoingStubbing *stubStepOne = MKTGiven([mockClass JSONObjectWithData:nil options:0 error:nil]);
id stubStepTwo = [stubStepOne withMatcher:anything() forArgument:1];
id stubStepThree = [stubStepTwo withMatcher:anything() forArgument:2];
[stubStepThree willReturn:mock([NSDictionary class])];
我猜问题出在错误参数上,因为它是通过引用传递的,而且我不相信我以前尝试过 stub 类似的东西。有谁知道如何让它发挥作用?
这里的目标是让 +[NSJSONSeralization JSONObjectWithData:option:error]
方法在从我的测试中调用时始终返回模拟 NSDictionary。
最佳答案
我以前没有使用过 OCMockito(最近一直在使用 OCMock),所以这只是一个猜测,但是您是否只是缺少第三个参数?
看起来 OCMockito 正在寻找数组中的第三个对象,并且看起来您只设置了两个参数。
如果你更换它是否有效:
[stubStepThree willReturn:mock([NSDictionary class])];
与
id stubStepFour = [stubStepThree withMatcher:anything() forArgument:3];
[stubStepFour willReturn:mock([NSDictionary class])];
关于ios - 在 OCMockito 中使用引用传递 stub 方法时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22105542/