小编lar*_*mba的帖子

OCMock - 部分模拟[UIAlertView alloc]

我遇到了OCMockiOS框架的问题.我基本上是试图嘲笑UIAlertViewinitWithTitle:message:delegate......方法.下面的示例在我调用initWithTitle方法时未返回存根返回值的意义上不起作用.

UIAlertView *emptyAlert = [UIAlertView new];
id mockAlert = [OCMockObject partialMockForObject:[UIAlertView alloc]];
[[[mockAlert stub] andReturn:emptyAlert] initWithTitle:OCMOCK_ANY message:OCMOCK_ANY delegate:nil cancelButtonTitle:OCMOCK_ANY otherButtonTitles:nil];

UIAlertView *testAlertReturnValue = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
if(testAlertReturnValue == emptyAlert) {
    NSLog(@"UIAlertView test worked");
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用相同的想法,它确实有效NSDictionary.

NSDictionary *emptyDictionary = [NSDictionary new];
id mockDictionary = [OCMockObject partialMockForObject:[NSDictionary alloc]];
[[[mockDictionary stub] andReturn:emptyDictionary] initWithContentsOfFile:OCMOCK_ANY];

NSDictionary *testDictionaryReturnValue = [[NSDictionary alloc] initWithContentsOfFile:@"test"];
if(testDictionaryReturnValue == emptyDictionary) {
    NSLog(@"NSDictionary test worked");
}
Run Code Online (Sandbox Code Playgroud)

我注意到的一件事是forwardInvocationForRealObject: …

iphone objective-c ocmock ios

3
推荐指数
1
解决办法
2644
查看次数

标签 统计

ios ×1

iphone ×1

objective-c ×1

ocmock ×1