iPhone-检索逻辑单元测试的资源 [英] iPhone - Retrieving Resources for logical unit tests

查看:57
本文介绍了iPhone-检索逻辑单元测试的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循Apple关于编写iPhone单元测试的文档,而且开箱即用,该例程似乎不起作用.我不确定我是否理解单元测试将在何处获得对应用程序委托的引用.

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.

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

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

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

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.

- (void) testAppDelegate {

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

}

另外:

我已经决定要进行逻辑测试.我正在尝试读取XML文件,但是没有解决这个问题的运气,这将为我提供访问文件的路径.我尝试粘贴allBundles输出的路径,但是该路径似乎也不起作用.以下是我在测试中执行的操作(您可以看到我用于输出包路径的调试语句):

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):

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");

本质上,路径上的assert不会成功,但是我不确定要在路径或目录中放置什么内容以引用我已告知要复制包资源的unitTest包.调用[NSBundle mainBundle]也不起作用.

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.

推荐答案

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

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"];

如果将其包含在作为单元测试包的一部分进行编译的类中,则该类将在单元测试包中查找文件SimpleTestList.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.

对于单元测试,只需确保设置复制捆绑包资源"以将plist包含在单元测试包中即可.

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

这篇关于iPhone-检索逻辑单元测试的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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