objective-c - 如何使用 Kiwi 异步测试委托(delegate)

标签 objective-c unit-testing ios6 kiwi

伙计们, 多年来,我一直在努力寻找一些关于如何使用 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/

相关文章:

c++ - boost 单元测试 : BOOST_CHECK_CLOSE returns 1. #INF%

ios - 在 UIRefreshControl 中将颜色更改为 attributedTitle

ios6 - 如何在 Xcode 中设置 passkit 权利

java - 测试应用程序生命周期、销毁和创建

xcode - <错误> : ImageIO: processAPP1 Failed to read tag #4 in mainIFD error in xcode log console

ios - 将已编辑的 UIImage 的更改保存到同一文件

ios - 使用 cocoapods 在 watch OS 2 上进行 AFNetworking

objective-c - 删除我的 mac 应用程序栏中的代理图标

objective-c - Xcode - 基于文档的应用程序(文档包)

Python 单元测试(使用 SQLAlchemy)不写入/更新数据库?