[英]Obj-C: How to get and call a block argument from NSInvocation - stubbing Twitter account on iOS
I'm testing an iOS application using KIF and OCMock, stubbing the device's ACAccountStore
to return my own representation of a Twitter account. 我正在使用KIF和OCMock测试iOS应用程序,将设备的
ACAccountStore
存根以返回我自己的Twitter帐户表示。 I want to stub requestAccessToAccountsWithType
, and call the passed completion handler with my own values, but I can't seem to get the block from the invocation and call it properly ( EXC_BAD_ACCESS
). 我想存根
requestAccessToAccountsWithType
,并使用我自己的值调用传递的完成处理程序,但我似乎无法从调用中获取块并正确调用它( EXC_BAD_ACCESS
)。 Being new to Objective-C and iOS, I'm sure I'm doing something wrong while pulling the block out of the NSInvocation
. 作为Objective-C和iOS的新手,我确信在将块移出
NSInvocation
时我做错了什么。
This is the production code. 这是生产代码。 The _accountStore is injected from the test setup.
_accountStore是从测试设置中注入的。
ACAccountType *twitterType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[_accountStore requestAccessToAccountsWithType:twitterType withCompletionHandler:^(BOOL granted, NSError *error) {
NSLog(@"authorized=%i in block", granted);
// important code here
}];
Test setup code. 测试设置代码。
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.