无法在 iPhone 单元测试中从 NSBundle 加载文件

发布于 2024-10-28 12:14:44 字数 795 浏览 4 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-11-04 12:14:44

在测试目标下,您不应该使用 [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.

揽月 2024-11-04 12:14:44

解决方案(没有意义)是不是在主要目标的捆绑资源中。即使我只在测试中使用它,它也必须在两个目标的捆绑资源中。

有人知道为什么是这种情况吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文