Is NSInvocation class not meant to be called via unit tests (for iPhone)?
My intent is to call a class’s method generically and since the method has more than 2 parameters, I can’t use [myTestClass performSelector:withObject:withObject]
Instead, I’m trying to use NSInvocation but as soon as I try to instantiate from NSInvocation class, I get the build error:
2009-12-31 11:12:16.380
otest[2562:903] ******* ABOUT TO CALL
NSInvocation
/Developer/Tools/RunPlatformUnitTests.include:
line 415: 2562 Bus error
“${THIN_TEST_RIG}”
“${OTHER_TEST_FLAGS}”
“${TEST_BUNDLE_PATH}”
/Developer/Tools/RunPlatformUnitTests.include:451:
error: Test rig
‘/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/
iPhoneSimulator3.1.2.sdk/Developer/usr/bin/otest’
exited abnormally with code 138 (it
may have crashed).
My class under test:
@implementation MyExampleClass
-(void)methodWithArgs:(NSString *)aValue
secondParam:(NSString *)aSecond
thirdParam:(NSString *)aThird
{
NSLog(@"methodWithArgs reached");
}
-(void)methodBlank
{
NSLog(@"methodBlank reached");
}
-(void)isTesting
{
NSLog(@"isTesting reached");
}
@end
My unit test:
@interface MyClassTests : SenTestCase
{
}
@end
@implementation MyClassTests
- (void)testNSInvocation
{
Class probeClass = NSClassFromString(@"MyExampleClass");
if (probeClass != Nil) {
SEL selector = NSSelectorFromString(@"isTesting");
NSMethodSignature *sig = [probeClass methodSignatureForSelector:selector];
// the following line causes the error
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
// this variation also fails
NSMethodSignature *sig2 = [probeClass
methodSignatureForSelector:@selector(methodWithArgs:secondParam:thirdParam:)];
NSInvocation *inv2 = [NSInvocation invocationWithMethodSignature:sig2];
}
}
@end
What’s a way to invoke a method with more than 2 parameters at run-time? Do I have to change the signature of the method so it only has 2 params? Is this a limitation of the unit-test framework?
The reason the above is failing is indeed due to an incorrect selector.
Instead of using “
methodSignatureForSelector“, I needed to specify: “instanceMethodSignatureForSelector“since the methods are instance methods.
However, both of your answers were helpful in tracking down the issue.