3

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
4

1 に答える 1

9

1ではなく3のインデックスを使用する必要があると思います。インデックス0はself、、インデックス1は_cmdです。

于 2011-11-29T23:37:42.677 に答える