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.
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.
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.
Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With