Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock ** parameter in OCMock in ARC

One of the parameter of my method is **error and my project is in ARC mode. When writing stub for this method to call a mock method i set parameter to below possible values. Either it causes compile error or failing to match the argument to call the mock method.

  1. OCMOCK_ANY
  2. [OCMArg anyPointer]
  3. Created a NSError object error and said [OCMArg setTo:error].

Nothing works.

How to mock such method? Please advice.

Edit

-(id)init{

    self = [super init];

    if (self) {
        id wcm = [OCMockObject partialMockForObject:self];
        [[[wcm stub] andCall:@selector(mockGetWakeupCallsForRoomNumber:error:)    onObject:self] getWakeupCallsForRoomNumber:OCMOCK_ANY error:((NSError __autoreleasing **)    [OCMArg anyPointer])];
        //[[[wcm stub] andCall:@selector(testMockMethod) onObject:self] testMethod];

    }
    return self;

}

-(void)testMethod
{
    NSLog(@"Original");
}

-(void)testMockMethod
{
    NSLog(@"Mock");
}
-(NSArray*)mockGetWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error
{
    @throw @"Mock method called";
}

-(NSArray*)getWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error
{
}

Mock on testMockMethod works.

like image 984
Saran Avatar asked Aug 17 '13 05:08

Saran


2 Answers

You can use setTo: or anyPointer if you cast it:

[[[mockFoo expect] andReturn:nil] someMethodWithError:((NSError __autoreleasing **)[OCMArg anyPointer])];

or:

NSError *error;
[[[mockFoo expect] andReturn:nil] someMethodWithError:((NSError __autoreleasing **)[OCMArg setTo:error])];

Here's a test case that passes with the method you've posted:

@interface Foo : NSObject{}

-(NSArray*)mockGetWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error;
-(NSArray*)getWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error;

@end

@implementation Foo

-(NSArray*)mockGetWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error
{
    return @[@"bar"];
}

-(NSArray*)getWakeupCallsForRoomNumber:(NSString*)roomNumber error:(NSError**)error
{
    return nil;
}

@end

@interface SomeTest : SenTestCase {}
@end

@implementation SomeTest

-(void)testMethod
{
    Foo *foo = [Foo new];
    id wcm = [OCMockObject partialMockForObject:foo];
    [[[wcm stub] andCall:@selector(mockGetWakeupCallsForRoomNumber:error:) onObject:foo] getWakeupCallsForRoomNumber:OCMOCK_ANY error:((id __autoreleasing *)[OCMArg anyPointer])];
    NSError *error;
    NSArray *calls = [wcm getWakeupCallsForRoomNumber:@"foo" error:&error];
    STAssertEquals(calls[0], @"bar", @"should match");
}

@end
like image 79
Christopher Pickslay Avatar answered Nov 19 '22 06:11

Christopher Pickslay


Finally this is what i implemented.

[
 [[wcm stub] 
           andCall:@selector(getWakeupCallsForRoomNumber:error:serverMessage:) 
   onObject:self]
   getWakeupCallsForRoomNumber:OCMOCK_ANY
                         error:((NSError *__autoreleasing *)[OCMArg anyPointer]) 
                 serverMessage:((NSString *__autoreleasing *)[OCMArg anyPointer])];
like image 2
Saran Avatar answered Nov 19 '22 07:11

Saran