ios - 在 OCMockito 中使用引用传递 stub 方法时出现异常

标签 ios pass-by-reference nsjsonserialization ocmockito

我正在尝试使用 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/

相关文章:

ios - 尝试打开相机 View 时 zbar 卡住

c++ - 谁应该清除通过引用检索的 vector ?

objective-c - 如何创建 json 并将其发布到 Web 服务 Objective c

iOS Autolayout - 在半透明导航栏下方正确定位 View

ios - 将数据追加到 UITableView 的正确方法,swift

ios - UIstoryboard:Modal segues 的 Animates 选项在 6.0 之前的 iOS 版本上不可用

matlab - 如何在函数中修改数组?

c++ - 难以理解 * 和 & 当它们与线程一起出现时

ios - 使用 NSJSONSerialization 时从 NSDictionary 获取 null

json - 使用 Swift 读取 JSON 文件