H guys, I have been trying for ages to find some good examples on how to use Kiwi testing to test delegate methods, asynchronously.
I have a manager class that defines the protocols for testing, with a pass and fail method returned in the delegate. Can anyone provide sample code on how to do this? Can I make the test class itself implement the to call the methods on the manager?
Thanks guys
You can do like in 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()];
});
});
});
Full example can be founded on this link.
You can do what I think you're trying to achieve by creating a mock object that stands in for the delegate, and then checking that the mock object receives the delegate callbacks that you expect. So the process would look like:
id delegateMock = [KWMock mockForProtocol:@protocol(YourProtocol)];
[manager setDelegate:delegateMock];
NSString *response = @"foo";
managerRepliedWithResponse
and foo
)[[[delegateMock shouldEventually] receive] managerRepliedWithResponse:response];
[manager performMyMethod];
The key is setting the expectation before you call the method, and using shouldEventually
which delays the assertion being checked and gives the manager
object time to perform the method.
There's a range of expectations you can also use that are listed on the Kiwi wiki - https://github.com/allending/Kiwi/wiki/Expectations
I've written the process up in more detail in a post on my site, albeit that it's more specifically geared-up to the situation I was dealing with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With