无法在 iPhone 单元测试中从 NSBundle 加载文件
我正在使用Xcode 4。
有人可以向我解释为什么当我写单元测试(在测试目标下)时找不到我的本地文件资源?我正在尝试在单元测试中加载NSBundle的PLIST文件,但在单元测试目标中不起作用。我尝试将其扔在测试目标的类区域下。
这是我的代码:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"urlMappings.plist"];
self.urlMappings = [NSDictionary dictionaryWithContentsOfFile:finalPath];
self.urlmappings由于某种原因是空的。这是urlmappings的来源。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>%server%view.html</key>
<string>testHtml1.html</string>
</dict>
</plist>
有什么想法吗?谢谢
I'm using XCode 4.
Can someone please explain to me why when I have written a unit test (under the test target) that it can't find my local file resource? I'm trying to load a plist file from the NSBundle in a unit test but it doesn't work in the unit test target. I've tried throwing it under the Classes area of the Test target.
Here's my code:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"urlMappings.plist"];
self.urlMappings = [NSDictionary dictionaryWithContentsOfFile:finalPath];
self.urlMappings is empty for some reason even though urlMappings.plist definately exists in my project. Here is the source to urlMappings.plist to prove it is in fact a dictionary.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>%server%view.html</key>
<string>testHtml1.html</string>
</dict>
</plist>
Any ideas? Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在测试目标下,您不应该使用 [NSBundle mainBundle],因为它是主包,而不是测试包。你应该使用 [NSBundle bundleForClass:[self class]] 替换。
Under the test target, you shouldn't use [NSBundle mainBundle], because it is the main bundle, not the test bundle. you should use [NSBundle bundleForClass:[self class]] replace.
解决方案(没有意义)是不是在主要目标的捆绑资源中。即使我只在测试中使用它,它也必须在两个目标的捆绑资源中。
有人知道为什么是这种情况吗?
The solution (which doesn't make sense) is it wasn't in the bundle resources of the main target. It has to be in the bundle resources of BOTH targets even though I'm only using it in test.
Anyone know why this is the case?