Unit testing of a static library that includes NSDocumentDirectory and other special applications for iOS applications

I am trying to run unit tests for a static library that is trying to create / write / read a file in the document directory. Since this is a static library, not an iOS app, trying to link to NSDocumentDirectory returns me the directory for the form

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

This directory does not exist. When trying to access a directory from a real application, NSDocumentDirectory returns something from the form:

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

(Note the simulator version as well as the application identifier as part of the path)

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

Thanks in advance.

+7
source share
4 answers

You can also solve this problem by mocking your access to the file so that the test confirms that your code tried to write the expected data to the path specified by NSDocumentsDirectory without even hanging the file system.

+3
source

I decide it for myself. In the unit test setup phase, I create the Document directory if it does not exist and delete it after the test completes. This effectively forces me to bypass the lock and continues my logic tests without worrying about the specifics of the iOS application.

+3
source

Use NSLibraryDirectory instead of NSDocumentDirectory for tests only specifying the TEST preprocessor macro .

Then delete the file in tearDown with:

 [[[NSFileManager alloc] init] removeItemAtURL:fileURL error:NULL] 
+2
source

The gh-unit structure can run unit tests in a simulator or on your device. This will not only solve your problem, but also allow you to debug your application (something I miss in other unit testing modules).

Download gh-unit

How to install gh-unit

0
source

All Articles