【问题标题】:Able to load file from app bundle but not from test app bundle能够从应用程序包加载文件,但不能从测试应用程序包加载
【发布时间】:2013-04-08 17:39:38
【问题描述】:

为了快速测试某些内容,我在我的应用程序包中添加了一个测试文件,并使用 NSBundle:pathForResource 将其加载到委托中。这行得通。

但是现在我刚刚创建了一个单元测试类型的目标并将文件和代码添加到这个新目标中,但无法加载它。

我已使用 Copy Bundle Resources 将文件添加到测试目标,这是加载它的完整代码:

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *xmlFilePath = [mainBundle pathForResource:@"TestServerResponse" ofType:@"xml"];
STAssertNotNil(xmlFilePath, @"Unable to open file TestServerResponse.xml")

我不知道为什么当它是 app bundle 目标的一部分时我可以加载它,但现在我已将它移到它不会加载的测试包中。

【问题讨论】:

    标签: iphone ios unit-testing nsbundle


    【解决方案1】:

    试试这个:

    NSBundle *testBundle=[NSBundle bundleForClass:[self class]];
    NSString *testBundlePath=[testBundle pathForResource:@"TestServerResponse" ofType:@"xml"];
    

    对于测试用例,主包或您的应用程序不是主包。

    您需要自己为您的班级访问捆绑包。

    【讨论】:

    • 谢谢,成功了。这两个调用之间有什么区别以及原始调用在测试包中不起作用的原因是什么?
    • main-bundle 和 bundle-for-self-class 之间的区别。 :)
    猜你喜欢
    • 2019-07-19
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    相关资源
    最近更新 更多