从 SenTestCase 派生类调用 NSInitation 不可行吗?
NSIncation 类不应该通过单元测试调用(对于 iPhone)吗?
我的目的是一般性地调用类的方法,并且由于该方法有超过 2 个参数,因此我不能使用 [myTestClass PerformSelector:withObject:withObject]
相反,我尝试使用 NSInitation 但作为当我尝试从 NSInitation 类实例化时,我收到构建错误:
2009-12-31 11:12:16.380 otest[2562:903] ****** 关于致电 NS调用 /Developer/Tools/RunPlatformUnitTests.include: 第 415 行:2562 总线错误
“${THIN_TEST_RIG}” “${OTHER_TEST_FLAGS}” “${TEST_BUNDLE_PATH}” /开发人员/工具/RunPlatformUnitTests.include:451: 错误:测试装置 '/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/ iPhoneSimulator3.1.2.sdk/Developer/usr/bin/otest' 异常退出,代码为 138( 可能已经崩溃)。
我的测试类:
@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
我的单元测试:
@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
在运行时调用具有两个以上参数的方法的方法是什么?我是否必须更改方法的签名,使其只有 2 个参数?这是单元测试框架的限制吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSIncation 在测试工具中应该可以正常工作。 SenTest 根本不干涉它。
这表明进程已崩溃。难的。
在调试器中运行测试工具并获取堆栈跟踪。我建议您立即进行测试以确保 sig 不为零。
NSInvocation should work fine within a test harness. SenTest doesn't muck with it at all.
This indicates that the process has crashed. Hard.
Run the test harness in the debugger and grab the stack trace. Off-hand, I would suggest that you test to make sure that
sig
is non-nil.您使用的选择器正确吗?您已指示测试将调用 MyExampleClass 中的 isTesting 方法,不带任何参数...就像 bbum 注释一样,选择器似乎不正确,因此 sig 为零,然后调用创建终止。您至少应该检查一下是否有这种情况,如果是这样就不要打电话。
Are you using the right selector? You have indicated the test is going to call a method isTesting in MyExampleClass, with no arguments... like bbum notes, it seems likely that selector is not right and therefor sig is nil and then invocation creation dies. You should at least check for that case and not call if it is so.
上述失败的原因确实是由于选择器不正确。
我需要指定:“
instanceMethodSignatureForSelector
”,而不是使用“methodSignatureForSelector
”,因为这些方法是实例方法。
但是,您的两个回答都有助于追踪问题。
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.