Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing of static library that involves NSDocumentDirectory and other iOS App specific calls

I'm attempting to run unit tests for a static library that attempts to create/write/read a file in the document directory. Since this is a static library and not an application for the iOS, attempts to reference the NSDocumentDirectory is returning me directory for the form

"/Users//Library/Application Support/iPhone Simulator/Documents"

This directory does not exist. When attempting to access a directory from an actual application, the NSDocumentDirectory returns something of the form:

"/Users//Library/Application Support/iPhone Simulator/4.2/FEDBEF5F-1326-4383-A087-CDA1B865E61A/Documents"

(Please note the simulator version as well as application ID as part of the path)

How can I overcome this shortcoming in the unit test framework for static libraries that implement tests that require iOS app specific calls?

Thanks in advance.

like image 997
Shiun Avatar asked Jan 04 '11 18:01

Shiun


3 Answers

You could also solve this by mocking your file access so that the test verifies that your code attempted to write the expected data to the path given by NSDocumentsDirectory without ever actually hitting the file system.

like image 51
Jonah Avatar answered Oct 04 '22 20:10

Jonah


I solve this for myself. In my unit test setup phase, I am creating the Document directory if it does not exist and removing it after the test finishes. This effectively gets me past the blockage and continues my logic tests without having to worry about iOS app specificity.

like image 23
Shiun Avatar answered Oct 04 '22 21:10

Shiun


Use NSLibraryDirectory instead of NSDocumentDirectory for tests only by defining a TEST preprocessor macro.

Then, remove the file in tearDown with:

[[[NSFileManager alloc] init] removeItemAtURL:fileURL error:NULL]
like image 31
ma11hew28 Avatar answered Oct 04 '22 21:10

ma11hew28