相关疑难解决方法(0)

为什么单元测试中的代码不能找到捆绑资源?

我单元测试的一些代码需要加载资源文件.它包含以下行:

NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"txt"];
Run Code Online (Sandbox Code Playgroud)

在应用程序中它运行得很好,但是当单元测试框架运行时pathForResource:返回nil,意味着它找不到foo.txt.

我已经确定它foo.txt包含在单元测试目标的Copy Bundle Resources构建阶段中,为什么它找不到该文件呢?

xcode cocoa unit-testing nsbundle octest

173
推荐指数
4
解决办法
4万
查看次数

应用程序包中的文件路径

我之前做了很多次,但现在失败了.我想访问我的应用程序中提供的文件.

NSString* path = [[NSBundle mainBundle] pathForResource:@"information" ofType:@"xml"];
Run Code Online (Sandbox Code Playgroud)

回报

/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
Run Code Online (Sandbox Code Playgroud)

这是正确的 - 在终端和所有这些检查.但是,尝试解析路径失败

    NSURL* fileURL = [NSURL URLWithString:path];

    NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];

    [nsXmlParser setDelegate:self];

    if(![nsXmlParser parse]){
        NSError* e = nsXmlParser.parserError;
        NSLog(@"ERROR parsing XML file: \n %@",e);
        self = NULL;
    }
Run Code Online (Sandbox Code Playgroud)

Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
Run Code Online (Sandbox Code Playgroud)

想法有人吗?

nsxmlparser ios ios7 swift

4
推荐指数
1
解决办法
2197
查看次数

标签 统计

cocoa ×1

ios ×1

ios7 ×1

nsbundle ×1

nsxmlparser ×1

octest ×1

swift ×1

unit-testing ×1

xcode ×1