OCMock and Values

magnifying glass shows a bug underneath
Credit: Pixabay / testbytes

OCMock is mostly great! And I use it a lot! But there’s one problem with it – handling values. I was debugging some asynchronous tests and having this problem, which I assumed at first came from the asynchronicity but it turns out, no, it’s just CGFloat being CGFloat.

Clue for this kind of bug is getting two causes of test failure: method unexpectedly called, and method expected but not called and they both look the same because to 6dp they are the same at 6dp but not quite beyond that. And there’s no way to set the margin of error.

One way to fix it is to get rid of CGFloats and use NSNumber instead, but I really didn’t want to do this. Sometimes you want to pass a value! And that should be fine!

It took me a while to figure this out, and I actually had to go back and use OCMock2 syntax to fix it [reference]. But: you can ignore non-object arguments. For some cases this would mean I wasn’t testing what I wanted to, but here I know that it being the correct CGFloat is covered elsewhere, so in this test I could safely ignore it.

[[[mock expect] ignoringNonObjectArgs]       someMethodWithIntArgument:0]

This made using andDo a bit weird (which because Asynchronous I was using to fulfil my expectation).  In the snippet below block is what gets called when the stub is invoked.

[[[[mock stub] andDo:block] ignoringNonObjectArgs] someMethodWithIntArgument:0];

Leave a Reply