【发布时间】:2011-11-29 23:34:11
【问题描述】:
我正在使用 KIF 和 OCMock 测试 iOS 应用程序,将设备的 ACAccountStore 存根以返回我自己的 Twitter 帐户表示。我想存根requestAccessToAccountsWithType,并用我自己的值调用传递的完成处理程序,但我似乎无法从调用中获取块并正确调用它(EXC_BAD_ACCESS)。作为 Objective-C 和 iOS 的新手,我确信在将块从 NSInvocation 中拉出时我做错了。
这是生产代码。 _accountStore 是从测试设置中注入的。
ACAccountType *twitterType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[_accountStore requestAccessToAccountsWithType:twitterType withCompletionHandler:^(BOOL granted, NSError *error) {
NSLog(@"authorized=%i in block", granted);
// important code here
}];
测试设置代码。
ACAccountStore *realStore = [[ACAccountStore alloc] init];
// Stub the store to return our stubbed Twitter Account
id mockStore = [OCMockObject partialMockForObject:realStore];
[[[mockStore stub] andDo:^(NSInvocation *invocation) {
void (^grantBlock)(BOOL granted, NSError *error) = nil;
[invocation getArgument:&grantBlock atIndex:1]; // EXC_BAD_ACCESS
grantBlock(TRUE, nil); // run the important code with TRUE
}] requestAccessToAccountsWithType:[OCMArg any] withCompletionHandler:[OCMArg any]];
// not shown: inject the mockStore into production code
【问题讨论】:
标签: objective-c ios ios5 objective-c-blocks ocmock