How to check delegates asynchronously with Kiwi

Guys, I’ve been trying for many years to find some good examples of how to use Kiwi testing to test delegation methods asynchronously.

I have a manager class that defines the protocols for testing, with the pass and fail method returned in the delegate. Can someone provide some sample code on how to do this? Can I make the test itself implement the method invocation method in the manager?

Thanks guys,

+6
source share
2 answers

You can do as in the example

SPEC_BEGIN(IFStackOverflowRequestSpec) describe(@"IFStackOverflowRequestSpec", ^ { context(@"question request", ^ { __block IFViewController *controller = nil; beforeEach(^ { controller = [IFViewController mock]; }); it(@"should conform StackOverflowRequestDelegate protocol", ^ { [[controller should] conformToProtocol:@protocol(StackOverflowRequestDelegate)]; }); it(@"should recieve receivedJSON", ^ { NSString *questionsUrlString = @"http://api.stackoverflow.com/1.1/search?tagged=iphone&pagesize=20"; IFStackOverflowRequest *request = [[IFStackOverflowRequest alloc] initWithDelegate:controller urlString:questionsUrlString]; [[request fetchQestions] start]; [[[controller shouldEventuallyBeforeTimingOutAfter(3)] receive] receivedJSON:any()]; }); it(@"should recieve fetchFailedWithError", ^ { NSString *fakeUrl = @"asda"; IFStackOverflowRequest *request = [[IFStackOverflowRequest alloc] initWithDelegate:controller urlString:fakeUrl]; [[request fetchQestions] start]; [[[controller shouldEventuallyBeforeTimingOutAfter(1)] receive] fetchFailedWithError:any()]; }); }); }); 

A complete example can be based on this link.

+6
source

You can do what I think you are trying to achieve by creating a mock object that stands for the delegate and then checking that the mock object receives the delegate responses you expect. Thus, the process will look like this:

  • Create a mock object that matches the delegate protocol:

id delegateMock = [KWMock mockForProtocol:@protocol(YourProtocol)];

  • set mock as a delegate of your manager class:

[manager setDelegate:delegateMock];

  • create an object containing the data that will be returned by your manager class:

NSString *response = @"foo";

  • establish a statement that the layout should be called using the method and the response object (in this case, I expect to get managerRepliedWithResponse and foo )

[[[delegateMock shouldEventually] receive] managerRepliedWithResponse:response];

  • method call:

[manager performMyMethod];

The key sets the expected value before the method is called and uses shouldEventually , which delays the checked statement and gives the manager object time to execute the method.

There are a number of expectations you can also use that are listed on the Kiwi wiki - https://github.com/allending/Kiwi/wiki/Expectations

I wrote the process in more detail in a post on my site , although it is more specifically oriented to the situation I was dealing with.

+4
source

All Articles