OCMock expect and return gives signature error
Asked Answered
S

2

6

I have a method of the followng signature;

- (NSInteger) getFirstVisitTimeStamp;

When I use OCMock to mock and return a value, the test fails with the below error.

[[[[YSNYI13NMockingTest mockedYI13N] expect] andReturnValue:@(12345)] getFirstVisitTimeStamp];

Error:

file:///%3Cunknown%3E: test failure:
failed: Return value does not match method signature; signature declares 'q' but value is 'i'.

Can anyone help ?

Sanburn answered 3/6, 2014 at 21:42 Comment(2)
Are q and i the actual values it lists?Rentfree
Are you running this on the 64 bit simulator?Bialy
B
9

On 64-bit devices, NSInteger is declared as long, but the value you are returning is being typed as int. Try forcing your value to a long by adding l after the number:

[[[[YSNYI13NMockingTest mockedYI13N] expect] andReturnValue:@(12345l)] getFirstVisitTimeStamp];
Bialy answered 3/6, 2014 at 21:52 Comment(1)
Well so..I spent like 40 mins trying to solve...and of curse.."q" means long long and "i" integer...XD Thanks a lot mate!Magnetometer
R
3

As @michaels hinted at in the comments, and after searching a bit further into your error message, this appears to be related to an open bug in OCMock.

It seems using OCMOCK_VALUE(...) might work for you instead.

Rentfree answered 3/6, 2014 at 21:51 Comment(4)
I tried using OCMOCK_VALUE(...) . it didn't work.Sanburn
Explicitly casting the number as a long, as suggested by @michaels, may work as well.Rentfree
It is a known problem. For a normal human all the numbers look the same. The runtime use many different number types, though. One approach to address this mismatch is to add code to OCMock to automatically convert numbers of different types. This is discussed in the open issue mentioned above. The problem is that when you look at all the edge cases, and the capabilities of the runtime, it's not straight-forward to decide what these conversions should be.Broider
@ErikDoernenburg, what about the non-normal humans? I know what you're saying, though. I'm not sure what the best solution would be, but it seemed that the OCMOCK_VALUE was suggested as a workaround.Rentfree

© 2022 - 2024 — McMap. All rights reserved.