OCMock class method for calling the passed block using custom data

I have a class that handles all my network communications as follows:

typedef void (^ networkEndblock) (NSArray *, NSError *);

@interface NetworkAPI : NetworkBase

+ (void) doSomeNetworkAcion:(networkEndblock) endBlock;

@end

I use the above code like this (I don't want to go into irrelevant details here)

- (void) runMyProcess:(SomeEndBlock)proccesEnded{

  // Bussiness logic


  // Get Data from the web
  [NetworkAPI doSomeNetworkAcion:^(NSArray *resultArray, NSError *error){

   // Check for error. if we have error then do error handling
   // If no error, check for array.
   // If array is empty then go to code that handles empty array
   // else Process data                                 

   }];
}

In my testing method, I want to run runMyProcess for testing, I do not want it to go and get on the network, I want to control this and set the cases so that it returns an error, an empty array ... etc. I know how to use SenTest, and this is MACROS, but I can’t fake my network API.

I looked at the stubs and expected, but I was embarrassed if I can do what I want.

thanks

+2
source share
1

mock [NetworkAPI doSomeNetworkAction:] andDo.

// This is in the OCMock project but is really useful
#import "NSInvocation+OCMAdditions.h"

// Whatever you want for these values
NSArray *fakeResultArray;
NSError *fakeError;

id networkAPIMock = [OCMockObject mockForClass:NetworkAPI.class];
[[[networkAPIMock expect] andDo:^(NSInvocation *invocation) {
    networkEndBlock endBlock = [invocation getArgumentAtIndexAsObject:2];
    endBlock(fakeResultArray, fakeError);
}] doSomeNetworkAction:OCMOCK_ANY];

, NetworkEndBlock typedef, .

+5

Source: https://habr.com/ru/post/1526924/


All Articles