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.
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.
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
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])];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With