hpique
hpique

Reputation: 120364

Unit testing methods with I/O in iOS

What are the best approaches to unit test methods that include I/O operations in iOS?

For example, I'm considering using Objective-C categories to redefine the behavior of I/O methods such as imageNamed. However, this prevents black-box unit testing because it's necessary to know which I/O methods are used in the implementation.

Upvotes: 3

Views: 235

Answers (2)

jlehr
jlehr

Reputation: 15617

Replacing method implementations in third-party frameworks is an extraordinarily risky thing to do, because there's no way to be sure of internal implementation details, and therefore dependencies you might be unknowingly breaking. It's also highly fragile -- even if it works correctly today, it might break something in a future release of the framework.

No amount of unit testing can guarantee the validity of changes you make this way. If you were doing stuff like this purely for experimentation, I'm sure that could be fun and might be useful for learning purposes, but then you wouldn't be worrying about black box testing.

Consider whether you could accomplish what you need by adding methods that parallel the existing ones, rather than replacing the framework's method implementations.

Upvotes: 1

zaph
zaph

Reputation: 112873

Either pass in the results of the I/O operation so that test data can be supplied in place of the actual I/O data or use OCMock. I have used OCMock for exactly this purpose. If you are considering OCMock, do read at least the header file for all the methods available.

If working with Legacy code consider reading/studying the book Working Effectively with Legacy Code by Michael Feathers

Upvotes: 1

Related Questions