【问题标题】:Obj-C: How to get and call a block argument from NSInvocation - stubbing Twitter account on iOSObj-C:如何从 NSInvocation 获取和调用块参数 - 在 iOS 上存根 Twitter 帐户
【发布时间】: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


    【解决方案1】:

    我认为您应该使用索引 3,而不是 1。索引 0 是 self,索引 1 是 _cmd

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 2011-05-11
    • 2013-04-08
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多