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

4

7

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.

Shilohshim answered 4/1, 2011 at 18:35 Comment(0)
D
3

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.

Durban answered 6/1, 2011 at 0:6 Comment(0)
S
3

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.

Shilohshim answered 5/1, 2011 at 23:50 Comment(1)
[[[NSFileManager alloc] init] removeItemAtURL:fileURL error:NULL]Vexed
D
3

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.

Durban answered 6/1, 2011 at 0:6 Comment(0)
V
2

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]
Vexed answered 21/7, 2011 at 5:24 Comment(0)
L
0

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

Liliuokalani answered 4/1, 2011 at 19:47 Comment(2)
To be fair to other third party testing tools it's really only xcode's built in SenTestingKit which doesn't support debugging tests and running them on either a device or in the simulator.Durban
In Xcode 4, you can set breakpoints in unit tests using SenTestingKit/OCUnit (the unit testing framework that comes with Xcode). Also, logic tests run in the simulator, and application tests run on the device, as explained in iOS Development Guide: Unit Testing Applications & other OCUnit references. See also: iOS Tests/Specs TDD/BDD and Integration & Acceptance Testing.Vexed

© 2022 - 2024 — McMap. All rights reserved.