使用KIF进行功能测试:加载视图控制器后调用beforeEach吗? [英] Feature tests with KIF: beforeEach is called after my view controller is loaded?

查看:123
本文介绍了使用KIF进行功能测试:加载视图控制器后调用beforeEach吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题(我想).

i have got simple (i guess) question.

我想使用Specta和KIF在我的应用程序中进行功能测试.问题是我在View Controller的viewDidLoad方法中设置了依赖关系,而在我的规范的beforeEach方法中,我只是为了不打入网络而注入了虚假对象.

I want to make a feature test in my app with Specta and KIF. The problem is that i am setting dependency in viewDidLoad method of my View Controller, and in beforeEach method of my spec i'm injecting fake object just to not hit the network.

结果是错误的,因为规格中的viewDidLoad在beforeEach方法之前被调用.

The result is wrong because viewDidLoad is being called before beforeEach method in specs.

在AppDelegate加载根视图控制器之前是否可以设置依赖项,以便一切都正确设置?

Is there a possibility to set dependencies before AppDelegate loads root view controller so everything is set up properly?

推荐答案

依赖于目标的测试(例如KIF和某些单元测试)在应用启动后启动,因此不能,您不能在AppDelegate之前使用beforeEach没有一些可怕的骇客.

Tests that depend on a target (like KIF and certain unit tests) launch after the app is launched so no, you can not make the beforeEach go before your AppDelegate without some terrible hackery.

我不知道您如何进行依赖注入,所以这是我们的方法/一些常规策略.

I don't know how you're doing dependency injection so here's how we do it/some general strategies.

理想情况下,KIF测试不应在代码级别进行模拟

这是因为KIF可以替代UIAutomation,并且主要用于UI级别的功能/功能测试.您实际上并不想太大地更改应用程序代码.使用网络的OHHTTPStubs或对象的OCMock这样的框架可以最好地实现模拟,当仅限于单元测试时,模拟效果最好.

This is because KIF is kind of an alternative to UIAutomation and mainly useful for feature/functional tests at the UI level. You don't really want to change your application code so much. Mocks are best achieved using frameworks like OHHTTPStubs for network or OCMock for objects and those work best when limited to unit tests.

如何在真实"应用中模拟网络请求

这里最好的方法是使用OHHTTPStubs或AMY服务器(由制作KIF的同一人制造)或Nocilla之类的东西返回存根响应.这样,您可以让您的应用程序代码完全运行.例如,OHHTTPStubs使用NSURLProtocol来拦截您的请求,因此从应用程序的角度来看,它几乎和出入网络一样好.

The best way here is to use something like OHHTTPStubs or AMY server (made by same people that made KIF) or Nocilla to return stub responses. This way you can let your app code run fully. OHHTTPStubs for example uses NSURLProtocol to intercept your requests so from the app perspective it's almost as good as going out to the network.

我真的很想模拟那些对象

如果您真的很想用不同的对象模拟依赖项注入的对象,那么会有更多或更少的hacky选项.

If you really really really want to mock out dependency injected objects with different objects then there are a couple of more and less hacky options.

1)使用真正的DI框架(或构建自己的DI框架)来修补依赖项.我用过台风,这很合理.这里的标准思想是利用控制反转来发挥自己的优势.因为要从应用程序上下文而不是直接获取所有对象,所以调整应用程序上下文抽象层要容易得多.台风甚至对此主题都有一个Wiki页面: https://github.com/appsquickly/Typhoon/wiki/集成测试

1) Use a real DI framework (or build your own) that allow for patching of the dependencies. I've used Typhoon and it's reasonable. The standard idea here is to use Inversion of Control to your advantage. Since you're getting all your objects from an application context rather than directly, it's much easier to tweak the application context abstraction layer. Typhoon even has a wiki page on this topic: https://github.com/appsquickly/Typhoon/wiki/Integration-Testing

2)跟踪要注入的对象的来源,并想在源处对其进行模拟和更改.这不是最优雅的方法,无论如何您都在破解一个DI框架,但也许您没有足够的时间或复杂性来使切换到DI框架值得.

2) Trace the source of the object you're injecting and want to mock out and change it at the source. This isn't the most elegant and you're sort of hacking out a DI framework anyway but maybe you don't have enough time or complexity to make switching to a DI framework worth it.

3)一直爬到顶层.有一个测试AppDelegate,它可以从常规AppDelegate继承而来,并在KIF测试期间使用它(当然,它可以存根或模拟出想要的对象).这不是很灵活,但是再次重申,也许您只想要一个测试用例或其他一些东西:

3) Hack all the way to the top level. Have a test AppDelegate that subclasses from your normal AppDelegate and use that during KIF testing (and of course have it stub out or mock out objects you want). This isn't flexible but again, maybe you just want one test case or something:

int main(int argc, char *argv[])
{
    int returnValue;
    @autoreleasepool {
        BOOL inIntegrationTests = NSClassFromString(@"KIFTestCase") != nil;
        if (inIntegrationTests) {
            returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegateForTest");
        }
        else {
            returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        }
    }
    return returnValue;
}

不幸的是,这并不是一个简单的我在哪里放置这种方法"的问题.

Ultimately this isn't a simple "where do I put this method" question unfortunately.

这篇关于使用KIF进行功能测试:加载视图控制器后调用beforeEach吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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