Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stub method block in Kiwi?
    text
    copied!<p>I want to stub a method which takes a block as a parameter using Kiwi. Here is the full explanation with code:</p> <p>I have a class named <code>TestedClass</code> which has a method <code>testedMethod</code> which dependent on class <code>NetworkClass</code> which calls via AFNetworking to a server, and return its response via block. Translating to code:</p> <pre><code>@interface TestedClass : NSObject -(void)testMethod; @end -(void)testMethod { NetworkClass *networkClass = [[NetworkClass alloc] init]; [networkClass networkMethod:^(id result) { // code that I want to test according to the block given which I want to stub ... }]; } typedef void (^NetworkClassCallback)(id result); @interface NetworkClass : NSObject -(void)networkMethod:(NetworkClassCallback)handler; @end -(void) networkMethod:(NetworkClassCallback)handler { NSDictionary *params = @{@"param":@", @"value"}; NSString *requestURL = [NSString stringWithFormat:@"www.someserver.com"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURLURLWithString:requestURL]]; NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:requestURL parameters:params]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { handler(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { handler(nil); }]; [operation start]; } </code></pre> <p>How can I use Kiwi to stub <code>networkMethod</code> with block in order to unit test <code>testMethod</code>?</p> <p>UPDATE: Found how to do this in Kiwi, see my answer below. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload