Shiun
Shiun

Reputation: 2677

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.

Upvotes: 7

Views: 2130

Answers (4)

ma11hew28
ma11hew28

Reputation: 126477

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]

Upvotes: 2

Jonah
Jonah

Reputation: 17958

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.

Upvotes: 3

Shiun
Shiun

Reputation: 2677

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.

Upvotes: 3

Tom Ilsinszki
Tom Ilsinszki

Reputation: 623

The gh-unit framework can run unit tests in the simulator or on your device. This will not only solve your problem, but let you debug your application (something I've been missing from other unit testing frameworks).

Download gh-unit

How to install gh-unit

Upvotes: 0

Related Questions