Question

I'm trying to use OCMockito to stub an NSJSONSerialization method. I thought I had a solution, but it turns out it causes this exception:

*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]

Here's what I was doing:

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])];

I'm guessing the problem is with the error argument, since that's meant to be passed by reference and I don't believe I've tried stubbing something like that before. Does anyone know a way to get this to work?

The goal here is to get the +[NSJSONSeralization JSONObjectWithData:option:error] method to always return a mock NSDictionary when called from my test.

No correct solution

OTHER TIPS

I haven't used OCMockito before (have recently been using OCMock) so this is just a guess, but are you simply missing the third argument?

Looks like OCMockito is looking for the third object in an array, and it looks like you have set only two arguments.

Does it work if you replace:

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

with

id stubStepFour = [stubStepThree withMatcher:anything() forArgument:3];
[stubStepFour willReturn:mock([NSDictionary class])];

OCMockito 1.2.0 (released April 5, 2014) now supports pointer arguments, including NSError**.

More info in the release notes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top