Xcode 单元测试 - 从应用程序包中访问资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2775510/
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
Xcode Unit Testing - Accessing Resources from the application's bundle?
提问by Ben Scheirman
I'm running into an issue and I wanted to confirm that I'm doing things the correct way.
我遇到了一个问题,我想确认我正在以正确的方式做事。
I can test simple things with my SenTestingKit tests, and that works okay. I've set up a Unit Test Bundle and set it as a dependency on the main application target. It successfully runs all tests whenever I press cmd+B.
我可以用我的 SenTestingKit 测试来测试简单的东西,而且效果很好。我已经设置了一个单元测试包并将其设置为对主应用程序目标的依赖。每当我按 cmd+B 时,它都会成功运行所有测试。
Here's where I'm running into issues. I have some XML files that I need to load from the resources folder as part of the application. Being a good unit tester, I want to write unit tests around this to make sure that they are loading properly.
这就是我遇到问题的地方。我有一些 XML 文件需要作为应用程序的一部分从资源文件夹加载。作为一名优秀的单元测试员,我想围绕此编写单元测试以确保它们正确加载。
So I have some code that looks like this:
所以我有一些看起来像这样的代码:
NSString *filePath = [[NSBundle mainBundle]
pathForResource:@"foo" ofType:@"xml"];
This works when the application runs, but during a unit test, mainBundle
points to the wrong bundle, so this line of code returns nil.
这在应用程序运行时有效,但在单元测试期间,mainBundle
指向错误的包,因此这行代码返回 nil。
So I changed it up to utilize a known class like this:
所以我改变了它以使用这样的已知类:
NSString *filePath = [[NSBundle bundleForClass:[Config class]]
pathForResource:@"foo" ofType:@"xml"];
This doesn't work either, because in order for the test to even compile code like this, it Config
needs to be part of the Unit Test Target. If I add that, then the bundle for that class becomes the Unit Test bundle. (Ugh!)
这也不起作用,因为为了让测试甚至编译这样的代码,它Config
需要成为单元测试目标的一部分。如果我添加它,那么该类的包将成为单元测试包。(啊!)
Am I approaching this the wrong way?
我是否以错误的方式接近这个?
回答by Nathan Eror
While trying to reproduce your problem it hit me: Have you tried just adding your resources to your test bundle? Would that cause any problems for you? I did exactly that, and it worked great for me. For example:
在尝试重现您的问题时,它击中了我:您是否尝试过将资源添加到测试包中?这会给你带来什么问题吗?我正是这样做的,它对我很有用。例如:
Code:
代码:
- (void)testBundleLoading {
NSLog(@"Main Bundle Path: %@", [[NSBundle mainBundle] bundlePath]);
for (NSBundle *bundle in [NSBundle allBundles]) {
NSLog(@"%@: %@", [bundle bundleIdentifier],
[bundle pathForResource:@"fire" ofType:@"png"]);
}
}
Output:
输出:
2010-05-05 14:31:05.961 otest[16970:903] Main Bundle Path: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin
2010-05-05 14:31:05.962 otest[16970:903] (null): (null)
2010-05-05 14:31:05.963 otest[16970:903] com.freetimestudios.KayakKingTests: /Volumes/FreeTime/KayakKing/build/Debug-iphonesimulator/KayakKingTests.octest/fire.png
I hope this helps.
我希望这有帮助。
回答by Ciryon
I ran into this problem with Xcode 4 and my problem was that I had an old Xcode 3 project and somehow the info plist for the test target had gone missing. I added a new UnitTests-Info.plist to the project and I was suddenly able to retrieve resources again.
我在 Xcode 4 中遇到了这个问题,我的问题是我有一个旧的 Xcode 3 项目,不知何故测试目标的信息 plist 丢失了。我在项目中添加了一个新的 UnitTests-Info.plist,我突然能够再次检索资源。