xcode iPhone - 为逻辑单元测试检索资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1092396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:39:10  来源:igfitidea点击:

iPhone - Retrieving Resources for logical unit tests

unit-testingxcoderesourcesocunit

提问by Gary

I've been following Apple's documentation on writing unit tests for the iPhone, and out of the box, the routine doesn't seem to work. I'm not sure I understand where the unit test is going to get the reference to the application delegate.

我一直在关注 Apple 关于为 iPhone 编写单元测试的文档,并且开箱即用,该例程似乎不起作用。我不确定我是否理解单元测试将在哪里获得对应用程序委托的引用。

My Dependencies are like the following: My_Program_target -> UnitTesting_target -> UnitTests_bundle

我的依赖项如下所示:My_Program_target -> UnitTesting_target -> UnitTests_bundle

The following code snippet is where the assert fails. I'm very familiar with CPPUNIT, but I'm having trouble understanding how this crosses over.

以下代码片段是断言失败的地方。我对 CPPUNIT 非常熟悉,但我无法理解它是如何交叉的。

- (void) testAppDelegate {

    id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
    STAssertNotNil(yourApplicationDelegate, @"UIAppliation failed to find the AppDelegate");

}

Additionally:

此外:

I've decided in my approach to do a logic test. I'm trying to read in an XML file, but I'm not having luck resolving the bundle, which will provide me with the path by which I can access my file. I've tried pasting in the path output by allBundles, but that path doesn't seem to work either. Below is what I'm executing in my test (you can see the debug statement I'm using to output the paths of the bundles):

我已经决定在我的方法中进行逻辑测试。我正在尝试读取一个 XML 文件,但我没有成功解析包,这将为我提供可以访问我的文件的路径。我已经尝试在 allBundles 输出的路径中粘贴,但该路径似乎也不起作用。下面是我在测试中执行的内容(您可以看到我用来输出包路径的调试语句):

NSLog(@"BundlePaths: %@", [NSBundle allBundles]);
   NSString * path = [[NSBundle bundleWithPath:@"$(TARGET_BUILD_DIR)"] pathForResource:@"SimpleTestList" ofType:@"plist"];
   STAssertNotNil(path, @"Bundle Location couldn't find the file specified");

Essentially, the assert on path is not successful, but I'm not sure what to put for the path or directory to reference my unitTest bundle that I've told to copy the bundle resources. Calling [NSBundle mainBundle] does not work either.

从本质上讲,路径上的断言不成功,但我不确定要为路径或目录放置什么来引用我已经告诉复制包资源的 unitTest 包。调用 [NSBundle mainBundle] 也不起作用。

回答by Gary

Ok, so I've figured it out. In order to open a file in a unit test, you'll need to specify the file to open as:

好的,所以我想通了。为了在单元测试中打开文件,您需要指定要打开的文件:

NSString * filePath = [[NSBundle bundleForClass:[self class] ] pathForResource:@"SimpleTestList" ofType:@"plist"];

If you include this in a class that's compiled as part of your unit test bundle, that class will look inside the unit test bundle for the file SimpleTestList.plist.

如果将它包含在作为单元测试包的一部分编译的类中,该类将在单元测试包中查找文件 SimpleTestList.plist。

For a unit test, just make sure you set up "Copy Bundle Resources" to include your plist in your unit test bundle.

对于单元测试,只需确保设置“复制捆绑资源”以将 plist 包含在单元测试捆绑中。

回答by Kendall Helmstetter Gelner

If you need the application delegate, you have to run the unit tests on the device itself and not the simulator. Also, you will see unit test output appear in the console, not in the build results.

如果您需要应用程序委托,则必须在设备本身而不是模拟器上运行单元测试。此外,您将看到单元测试输出出现在控制台中,而不是构建结果中。

The key thing to know is that there are two types of unit tests - logic tests that are run outside of the executable, and then integrated system kinds of tests that need the full running environment.

要知道的关键是有两种类型的单元测试 - 在可执行文件之外运行的逻辑测试,然后是需要完整运行环境的集成系统类型的测试。

The logic tests MUST be run with the simulator selected as the target or they will not run.

逻辑测试必须在选择为目标的模拟器的情况下运行,否则它们将不会运行。

The integrated system tests MUST be run as part of the executable, on the device - you'll want a new target to accomplish this.

集成系统测试必须作为可执行文件的一部分在设备上运行 - 您需要一个新目标来完成此操作。

Sorry this is all so complex, this aspect is still very much a work in progress compared to many other unit testing frameworks.

抱歉,这一切都太复杂了,与许多其他单元测试框架相比,这方面的工作仍在进行中。

回答by Frederick Squid

The Swift 3 translation of Gary's answer above (using an URL instead of a string path) is:

上面加里的答案的 Swift 3 翻译(使用 URL 而不是字符串路径)是:

let url = Bundle(for: type(of: self)).url(forResource: "SimpleTestList", withExtension: "plist")

Notice the critical and non-obvious part type(of: self)instead of [self class].

注意关键和不明显的部分,type(of: self)而不是[self class]