2013-10-15 23 views
2

我有以下签名的方法,我想用OCMock的存根功能测试:OCMock和块

- (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block 

我怎么会去嘲笑他地处理返回块。

我曾尝试:

[[_mockAuthenticationRepository stub] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:^(GNCustomer *customer, NSError *error) { 

      }]; 

当我尝试用这种方法存根,我收到了意想不到的调用方法,这表明不使用我的存根。

谢谢!

回答

5

好吧,我想通了这一点:)这里的答案:

[[[_mockAuthenticationRepository stub] andDo:^(NSInvocation *invocation) { 
       void (^successBlock)(GNCustomer *customer, NSError *error) = nil; 

       [invocation getArgument:&successBlock atIndex:4]; 

       NSDictionary *details = @{ NSLocalizedDescriptionKey : [OCMArg any] }; 
       NSError *error = [NSError errorWithDomain:@"Some Domain" code:401 userInfo:details]; 

       successBlock(nil, error); 
      }] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:[OCMArg any]]; 
+1

的atIndex选项也指方法签名块被调用开始的2对第一个参数的索引点。 – strickland