Xcode中的单元测试,是否运行应用程序? [英] Unit Testing in Xcode, does it run the app?
问题描述
我遇到了一个我之前没遇到过的奇怪问题。
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方法,app / 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屋!