8

I have a category on NSString class that contains a private helper method. It would be handy if I could use this method in my unit test. However I have difficulties to expose it. When I create a class extension on NSString and declare the method here, the method is not visible in unit test. And it doesn't matter if I create the class extension in a separate header file, or as a part of unit test .m file.

It looks like I am missing something here.

Any help guys?

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Earl Grey
  • 7,426
  • 6
  • 39
  • 59
  • Not an answer, but general info, see here: http://stackoverflow.com/questions/856115/should-one-test-internal-implementation-or-only-test-public-behaviour – CouchDeveloper Jan 22 '14 at 18:04

2 Answers2

15

Common unit testing guidance would tell you not to try and test your private methods. Only test via your public interfaces. Private methods are simply an implementation detail that could change at any time, when you refactor. Your public interfaces should be pretty stable, and will exercise your private methods.

However, if you still want to test your private category methods, the following works for me...

First, your category:

UIImage+Example.h

@interface UIImage (Example)    
@end

UIImage+Example.m

@implementation UIImage (Example)

+ (NSString *)examplePrivateMethod
{
    return @"Testing";
}

@end

MyExampleTests.m

#import <XCTest/XCTest.h>
#import "UIImage+Example.h"

@interface UIImage (Example_Test)
+ (NSString *)examplePrivateMethod;
@end

@interface MyExampleTests : XCTestCase
@end

@implementation MyExampleTests

- (void)testExample
{
    XCTAssertEqualObjects(@"Test", [UIImage examplePrivateMethod], @"Test should be test");
}

@end

Essentially, redeclare your private method in a new category in your test. However, as mentioned above this is exposing private methods just for the purpose of testing, and coupling your tests to your implementation.

James Frost
  • 6,960
  • 1
  • 33
  • 42
  • Hm..I'll definitely will take a look at the overall code design before I resort to that solution. But thanks. – Earl Grey Jan 22 '14 at 22:42
5

You can execute any method (private or not) on an object by simply using performSelector: on it, like so:

[something performSelector:@selector(somePrivateMethod)];

But I agree with James that you should only do that when absolutely necessary.

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165