Using a struct with OCMock or Hamcrest
Asked Answered
V

4

9

I'm hitting a road block and I'm wondering if the brilliant collective minds here can help. In ObjC CocoaTouch I'm trying to mock an object that takes struct parameters and returns a struct. OCMock is coughing up a hair-ball so I tried wrapping with a Hamcrest matcher. No die. The function/method I'm testing looks something like this:

- (CLLocationCoordinate2D)pixelToLatLong:(CGPoint)aPoint;

I use code like this:

#define OCMOCK_STRUCT(atype, variable) [NSValue value:&variable withObjCType:@encode(atype)]
-(void) testMyWidget
{
    CLLocationCoordinate2D ulLL = (CLLocationCoordinate2D){123,456};
    CLLocationCoordinate2D lrLL = (CLLocationCoordinate2D){654,321};
    [[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, ulLL)] pixelToLatLong:(CGPoint){0,0}];
    [[[(id)myObj expect] andReturn:OCMOCK_STRUCT(CLLocationCoordinate2D, lrLL)] pixelToLatLong:(CGPoint){320,460}];//lower right point
}

That kinda works. So in my object that I'm testing I make the necessary required edits to get a green bar... err.. green button in the build info window. When I'm certain that my test should pass I get assertion failed errors. The errors inform me that the method was invoked unexpectedly and lists the values for these structs as question marks. I tried wrapping the structs with Hamcrest matchers but I'm getting nowhere. I'm getting ready to break out my debugger which will no doubt show me what's wrong. Has anybody here had similar trouble with OCMock/Hamcrest and structs? If so, what's the best way to handle these types?

Vaginate answered 5/2, 2009 at 15:24 Comment(0)
F
3

You're very close. Your #define should be:

#define OCMOCK_STRUCT(atype, variable) [NSValue valueWithBytes:&variable withObjCType:@encode(atype)]
Frow answered 7/6, 2010 at 14:42 Comment(0)
B
3

The best answer is actually Cliff's himself: http://codeforfun.wordpress.com/2009/02/07/ocmock-return-a-struct/

He just didn't update this question, shame shame :)

Braeunig answered 21/2, 2011 at 1:9 Comment(0)
M
2

I had problems with the macro answer; writing a helper function that returned the struct in the testing class and using:

[[[mockObject stub] andCall:@selector(selectorName) onObject:self] someMethod];

worked really well.

Meatus answered 30/9, 2010 at 16:50 Comment(0)
R
0

Sometimes a hand-coded mock is easier than trying to coerce a mock object framework outside of its normal use patterns.

Raid answered 9/1, 2010 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.