簡體   English   中英

Obj-C:如何從NSInvocation獲取並調用塊參數 - 在iOS上存根Twitter帳戶

[英]Obj-C: How to get and call a block argument from NSInvocation - stubbing Twitter account on iOS

我正在使用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

我認為你應該使用索引3而不是1.索引0是self ,索引1是_cmd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM