涉及Mknetworkkit的iOS写作单元测试 [英] iOS writing unit test involving Mknetworkkit

查看:94
本文介绍了涉及Mknetworkkit的iOS写作单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的项目中运行MKNetworkKit,这似乎很好.然后,我创建了一个单元测试项目,并调用引擎发出一些请求(使用MkNetworkOperation).我没有看到明显的错误,但是从未调用completionhandler块,因此errorblock也是如此.

I tried to run MKNetworkKit in my project, it seemed to be fine. I then created a unit test project and call the engine to make some requests (using the MkNetworkOperation). I get no apparent error but the completionhandler block never get called, and so was the errorblock.

我检查了我的单元测试设置,没有明显的错误,整个过程都运行了".我只是没有得到任何回应.如果我切换并在我的主项目中执行此操作,则它可以工作.

I examined my unit test setup, there's no apparent error, and the whole thing "ran". I just dont get any response. If i switch and do this in my main project, it works.

我还将所有必要的框架添加到了单元测试项目中.

I have also added all the necessary framework to the unit test project.

我注意到的一件事是,针对有效的情况打印了可达性消息,而对于无效的情况则什么也没有打印出来.

One thing i notice is that Reachability message get printed out for the case that worked, but nothing for the case that didnt.

关于发生了什么的任何线索吗?

Any clue as to whats going on?

推荐答案

听起来像您需要一个信号灯或类似的东西来阻止执行,直到请求返回.单元测试将一直运行到方法结束.如果他们在没有错误的情况下到达方法的末尾,则说明它们成功.由于您使用的是MKNetworkKit,因此服务器请求是异步的,因此在请求完成之前会命中该方法的结尾.我找到了一个帮助器类来帮助进行单元测试.我将尝试再次找到它,并使用一些示例代码链接到它.

Sounds like you need a semaphore or something similar to block the execution until the request returns. Unit tests run until the end of the method. If they hit the end of the method without an error, they were successful. Since your using MKNetworkKit, the server request is async, so the end of the method is hit before the request is complete. I found a helper class to help with unit tests. I'll try to find it again and link to it with some sample code.

找到了它: http://www. touch-code-magazine.com/unit-testing-for-blocks-based-apis/

自从我编写这些测试以来已经有一段时间了.我很确定这是可行的,但是您可能需要对其进行调整.

So it's been a while since I wrote these tests. I'm pretty sure this will work, but you might have to tweak it.

-(void)tests {
    //setup
    NSString *key = @"myTestKey";

    //test
    STAssertNoThrow(
        [API resetPassword:@"fakeemail@this.net callback:^(NSDictionary *result) {

        STAssertNotNil(result, @"reset pass response was nil"]);
        [[TestSemaphor sharedInstance] lift:key];
    } error:^(NSDictionary *error) {

        STFail(@"reset password failed: %@", error.description);
        [[TestSemaphor sharedInstance] lift:key];
    }], @"reset password failed");

    [[TestSemaphor sharedInstance] waitForKey:key];
}

这篇关于涉及Mknetworkkit的iOS写作单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆