Xcode 中的单元测试,它是否运行应用程序? [英] Unit Testing in Xcode, does it run the app?

查看:17
本文介绍了Xcode 中的单元测试,它是否运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个以前没有遇到过的奇怪问题.

I'm running into a strange problem that I haven't run into before.

当您执行 cmd+U 来运行单元测试(例如 OCUnit)时,它是否真的调用了 main.m、新建了 appDelegate 并运行应用程序,就像您按下了 cmd+R 一样?

When you do cmd+U to run your Unit Tests (OCUnit for example) does it actually call the main.m, new up the appDelegate and run the app as if your had pressed cmd+R?

我之所以这么问是因为我在这个 DataLayer 后面使用了 CoreData.我在我的测试中成功模拟了 DataLayer,但是一旦我实现了一个实际调用 CoreData 的 getAll 方法,应用程序/xcode 就会抛出一个关于托管对象模型的异常不能为零.我理解,但我并不打算真正更新 DataLayer 类,并且我在 mainviewcontroller loadView 方法中放置了一个断点,它调用 DataLayer getAll 方法.测试应该无关紧要,因为这是一个模拟对象,但它显然是在调用真实实例.

I only ask because I'm using CoreData behind this DataLayer. I'm mocking out the DataLayer successfully in my tests, but once I implemented a getAll method that is actually calling CoreData, the app/xcode is throwing an exception about the managed object model can't be nil. Which I understand, but I'm not meaning to actually new up the DataLayer class, and I've put a break point in my mainviewcontroller loadView method where it is calling the DataLayer getAll method. It shouldn't matter with tests because this is a mock object, but it's apparently calling the real instance.

回到我的问题,当按下 cmd+U 时,它是否也先运行应用程序然后运行测试?

So back to my question, when pressing cmd+U does it also run the app first then run the tests?

推荐答案

该应用程序实际上已运行,但您可以使用一个技巧来阻止它运行.

The application is actually run but there is a trick you can use to prevent it from running.

int main(int argc, char* argv[]) {
    int returnValue;

    @autoreleasepool {
        BOOL inTests = (NSClassFromString(@"SenTestCase") != nil
                     || NSClassFromString(@"XCTest") != nil);    

        if (inTests) {
            //use a special empty delegate when we are inside the tests
            returnValue = UIApplicationMain(argc, argv, nil, @"TestsAppDelegate");
        }
        else {
            //use the normal delegate 
            returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        }
    }

    return returnValue;
}

这篇关于Xcode 中的单元测试,它是否运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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