Reputation: 129
I do have a class that handles all my network communication like this:
typedef void (^ networkEndblock) (NSArray *, NSError *);
@interface NetworkAPI : NetworkBase
+ (void) doSomeNetworkAcion:(networkEndblock) endBlock;
@end
I use the above code like this (I do not want to get 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 test method I want to trigger runMyProcess to test, I do not want it to go and hit the network, I want to control that and set cases to make it return an error, empty array ...etc. I know how to use SenTest and it is MACROS but I do not how to fake my network API.
I looked into stubs and expect but I got confused if I can do what I want.
Thanks
Upvotes: 2
Views: 1637
Reputation: 18922
Class mock [NetworkAPI doSomeNetworkAction:]
and use an andDo
block.
// 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];
Also, I would capitalize NetworkEndBlock
in my typedef as it will make your code easier to read for others.
Upvotes: 6