Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL to file path in test bundle with XCTest

I am trying to write an iOS app using TDD and the new XCTest framework. One of my methods retrieves a file from the internet (given a NSURL object) and stores it in the user's documents. The signature of the method is similar to:

- (void) fetchAndStoreImage:(NSURL *)imageUrl 

I'm trying to write the test for this method in a way that it doesn't fail if there is no connection to the internet. My approach (taken from a previous question) is to call the method using a NSURL to an image in the local file system.

When a new project with unit tests enabled is created, the Tests directory has a subdirectory named 'Supporting Files'. I suppose that is where my test images should go. My question is how can I get a NSURL object that points to an image in this directory, as I wouldn't want test images to be bundled with the application. Any help is appreciated.

like image 647
Eduardo Arenas Prada Avatar asked Oct 11 '13 02:10

Eduardo Arenas Prada


People also ask

What is XCTest bundle?

xctest bundles are loaded at runtime by the process that they are being injected into, just like a plugin or shim. The dynamic library within the . xctest bundle links with XCTest. framework and can also link with other System-Level Frameworks, or code that is statically linked inside the test bundle.

How do I run XCTest in Xcode?

Test Lab runs unit tests and UI tests using the XCTest framework. To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.

Is XCTest a unit testing framework?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.


1 Answers

In fact, the [NSBundle mainBundle] when running a UnitTest is not the path of your app, but is /Developer/usr/bin, so this will not work.

The way to get resources in a unit test is here: OCUnit & NSBundle

In short, use:

[[NSBundle bundleForClass:[self class]] resourcePath] 

or in your case:

[[NSBundle bundleForClass:[self class]] resourceURL] 
like image 165
Ben Clayton Avatar answered Nov 07 '22 16:11

Ben Clayton