本文介绍了OCMock和块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有以下签名的方法,我想使用OCMock的存根功能进行测试:
I have a method with the following signature that I want to test using OCMock's stub feature:
- (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block
我该如何模拟它来处理返回的块.
How would I go about mocking this to handle the returned block.
我尝试过:
[[_mockAuthenticationRepository stub] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:^(GNCustomer *customer, NSError *error) {
}];
当我尝试使用这种方法进行存根操作时,我收到了意外的方法调用,这表明我的存根未被使用.
When I try to stub with this approach, I received unexpected method invoked which indicates that my stub is not being used.
谢谢!
推荐答案
好了,我明白了:)这是答案:
Well I figured this out :) Here's the answer:
[[[_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]];
这篇关于OCMock和块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!