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