2013-10-15 43 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