3

我正在尝试使用 OCMockito 来存根一个NSJSONSerialization方法。我以为我有一个解决方案,但事实证明它会导致此异常:

*** -[__NSArrayM objectAtIndex:]:索引 2 超出范围 [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])];

我猜问题出在错误参数上,因为它是通过引用传递的,我不相信我以前尝试过存根类似的东西。有谁知道让这个工作的方法?

这里的目标是让该+[NSJSONSeralization JSONObjectWithData:option:error]方法在从我的测试中调用时始终返回一个模拟 NSDictionary。

4

2 回答 2

1

OCMockito 1.2.0(2014 年 4 月 5 日发布)现在支持指针参数,包括 NSError**。

发行说明中的​​更多信息。

于 2014-04-07T20:33:47.637 回答
1

我之前没有使用过 OCMockito(最近一直在使用 OCMock)所以这只是一个猜测,但你只是错过了第三个参数吗?

看起来 OCMockito 正在寻找数组中的第三个对象,并且看起来您只设置了两个参数。

如果您更换它是否有效:

[stubStepThree willReturn:mock([NSDictionary class])];

id stubStepFour = [stubStepThree withMatcher:anything() forArgument:3];
[stubStepFour willReturn:mock([NSDictionary class])];
于 2014-03-22T03:44:50.470 回答