我的方法的一个参数是**错误,我的项目是在ARC模式.当为此方法编写存根以调用mock方法时,我将参数设置为可能的值.它会导致编译错误或无法匹配参数来调用mock方法.
> OCMOCK_ANY
> [OCMArg anyPointer]
>创建一个NSError对象错误,并表示[OCMArg setTo:error].
没有什么工作.
如何模拟这种方法?请指教.
编辑
-(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 { }
模拟testMockMethod工作.
解决方法
您可以使用setTo:或anyPointer来转换它:
[[[mockFoo expect] andReturn:nil] someMethodWithError:((NSError __autoreleasing **)[OCMArg anyPointer])];
要么:
NSError *error; [[[mockFoo expect] andReturn:nil] someMethodWithError:((NSError __autoreleasing **)[OCMArg setTo:error])];
这是一个通过您发布的方法传递的测试用例:
@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