Load files in xcode unit tests

前端 未结 7 2013
失恋的感觉
失恋的感觉 2021-02-02 05:17

I have an Xcode 5 unit test project and some test xml files related to it. I\'ve tried a bunch of approaches but I can\'t seem to load the xml files.

I\'ve tried the fol

7条回答
  •  孤城傲影
    2021-02-02 06:00

    Relative paths are relative to the current working directory. By default, that's / — the root directory. It's looking for that folder at the root level of your startup disk.

    The correct way to get a resource that's within your bundle is to ask your bundle for it.

    In an application, you'd get the bundle using [NSBundle mainBundle]. I don't know if that works in a test case; try it, and if it doesn't (if it returns nil or an unuseful bundle object), substitute [NSBundle bundleForClass:[self class]].

    Either way, once you have the bundle, you can ask it for the path or URL for a resource. You generally should go for URLs unless you have a very specific reason to need a path (like passing it to a command-line tool using NSTask). Send the bundle a URLForResource:withExtension: message to get the resource's URL.

    Then, for the purpose of reading a string from it, use [NSString stringWithContentsOfURL:encoding:error:], passing the URL you got from the bundle.

提交回复
热议问题