伙计们, 多年来,我一直在努力寻找一些关于如何使用 Kiwi 测试来异步测试委托(delegate)方法的好例子。
我有一个管理器类,它定义了测试协议(protocol),并在委托(delegate)中返回了通过和失败方法。任何人都可以提供有关如何执行此操作的示例代码吗?我可以让测试类本身实现以调用管理器上的方法吗?
谢谢大家
最佳答案
你可以像例子那样做
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()];
});
});
});
完整示例可以在 this link. 上找到
关于objective-c - 如何使用 Kiwi 异步测试委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13616255/