Unit testing private method - objective C Unit testing private method - objective C ios ios

Unit testing private method - objective C


Methods in Objective-C are not really private. The error message you are getting is that the compiler can't verify that the method you are calling exists as it is not declared in the public interface.

The way to get around this is to expose the private methods in a class category, which tells the compiler that the methods exist.

So add something like this to the top of your test case file:

@interface SUTClass (Testing)- (void)somePrivateMethodInYourClass;@end

SUTClass is the actual name of the class you are writing tests for.

This will make your private method visible, and you can test it without the compiler warnings.


A little late, but I just got onto the TDD train.

Private methods shouldn't be tested. Because you write private methods to support your public methods, thus testing your public methods indirectly tests the private methods which support them.

The principle "private methods shouldn't be tested" is supported by the principle "when you need to test private methods, it probably means that you should move those methods to the separate class", thus making them public.


If a method is private, you never should test it.

Think about this. You should test behaviour and contract of your methods instead internal implementation