我正在尝试使用 OCMock,但到目前为止我的简单实验失败了。鉴于我想模拟的这个实验课:
@interface HTTPConnection : NSObject <NSURLConnectionDelegate>
@property (strong, nonatomic) NSURLConnection *connection;
-(int) makeConnection:(NSString*) url; // implementation returns 5
@end
当我调用以下测试用例时,结果变量包含 5 而不是 15,因此调用的是真实对象方法而不是模拟方法:
- (void)testExample
{
id connection = [OCMockObject mockForClass:[HTTPConnection class]];
[[[connection stub] andReturn: [NSNumber numberWithInt: 15]] makeConnection:[OCMArg any]];
HTTPConnection* realConnection = [[HTTPConnection alloc] init];
int result = [realConnection makeConnection:@"http://www.anything.com"];
}