1

I'm trying to write some test cases for my iOS app using XCode 5 and the respective test templates. My app uses IAPs which I can't test in Simulator - but I want to be able to run tests on Simulator (I do have devices, but that's beside the point for me).

Pretend I have this:

// IAPController.m
-(void)aFunction:(NSObject*)aParam
{
   // do something
}

Note that aFunction is not declared in IAPController.h.

How can I call this function regardless of its "anonymous" nature in my testcase (which imports IAPController.h)? I mean I know the function is there, and I know Obj-C doesn't really support private functions, but still (with ARC...) it's basically not allowing me to. I was trying to utilize NSInvocation, is this the right direction?

Undo
  • 25,519
  • 37
  • 106
  • 129
pille
  • 1,411
  • 1
  • 15
  • 18

3 Answers3

1

Since you need to expose your methods to test classes, one of the possible solutions will be to declare those methods in class extension separate "protected" header file:

// IAPController_protected.h
@interface IAPController()
-(void)aFunction:(NSObject*)aParam;
@end

And import that method only in test files. That way you have clear separation between public interface and "protected" interface required for unit tests.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
0

You can use

- (id)performSelector:(SEL)aSelector
       withObject:(id)anObject

if the methods' arguments are objects, otherwise use NSInvocation.

vojer
  • 249
  • 1
  • 7
0
SEL selector = NSSelectorFromString(@"aFunction:");
IAPController *controller = [IAPController new];
NSObject *aParam = [NSObject new];
[controller performSelector:selector withObject:aParam];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161