我试图更改现有的defaultTestSuite方法,以创建一个可以从不同类中选择测试方法并以特定顺序执行它们的类。但是,每次我将测试文件导入另一个测试文件时,都会出现链接器错误
duplicate symbol _OBJC_METACLASS_$_TMKTestExample
为什么OCUnit会发生这种情况,我该如何解决?
问候
我通过在属于SenTestCase的testInvocation方法中创建自己的NSInvocations来找到了解决方案。
基本上,我有一个空的测试类,该类抛出引发执行某些操作(用于流测试)的测试方法,这些方法在每个NSInvocation中设置,一旦测试运行,这些方法将按照与testInvocation static中存在的顺序相同的顺序执行方法,它允许添加尽可能多的方法,类似于下面的原始示例:
扩展SenTestCase类
- (id) initWithInvocation:(NSInvocation *)anInvocation
{
id invocationTarget = anInvocation.target;
self = [super initWithInvocation:anInvocation];
if (!self) {
return nil;
}
if (invocationTarget != nil && invocationTarget != self) {
anInvocation.target = invocationTarget;
}
return self;
}
有必要重写上面的方法,因为在超级initWithInvocation方法中,目标始终被设置为self。
+(NSArray *) testInvocations
{
NSMutableArray *invocations = (NSMutableArray *)[super testInvocations];
TMKTestFirstViewControllerBaseAction *baseAction = [TMKTestFirstViewControllerBaseAction sharedInstance];
for (int i=0; i<4; i++) {
NSInvocation *invocation = nil;
switch (i) {
case 3:{
invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapChangeBackgroundButton)];
break;
}
case 2:{
invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapChangeBackgroundButton)];
break;
}
case 0:{
invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapBarButtonWithAccessibilityLabel:)];
NSString *arg = @"Second";
[invocation setArgument:&arg atIndex:2];
break;
}
case 1:{
invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapBarButtonWithAccessibilityLabel:)];
NSString *arg = @"First";
[invocation setArgument:&arg atIndex:2];
break;
}
default:
break;
}
[invocation retainArguments];
NSLog(@"invocation target: %d target: %@", i,invocation.target);
[invocations insertObject:invocation atIndex:i+1];
}
return invocations;
}
上面的示例仅添加了一个测试,但是有可能看到我的意思,这取自这些网站中的示例:
参数测试:http : //briancoyner.github.io/blog/2011/09/12/ocunit-parameterized-test-case/
nsinvocation声明:https : //github.com/aleph7/a-coding/blob/master/ObjC/Invocations/NSInvocation%2BConstructors.m
通过使用这两种方法并控制测试顺序,我能够为KIF创建以下内容:-描述在特定UIViewController / UIView / etc。中进行测试的操作的类,然后重用这些方法来创建流程测试,回归UI从代码或脚本进行测试(仍在进行脚本测试)。-正常的考试课程。
我希望这对需要此非常具体要求的人有所帮助。
关于最初的问题,我还没有弄清楚,我想知道是否有办法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句