1

我有一个测试用例和一个助手类。在助手类中,我也想使用断言,如下所示:

MainTests.h

#import <SenTestingKit/SenTestingKit.h>

@interface MainTests : SenTestCase

@end

MainTests.m

#import "MainTests.h"
#import "HelperClass.h"

@implementation MainTests

- (void)testExample {
    HelperClass *helperClass = [[HelperClass alloc] init];
    [helperClass fail];
}

@end

助手类.h

#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

- (void)fail;

@end

助手类.m

#import "HelperClass.h"

@implementation HelperClass

- (void)fail {
    STFail(@"This should fail");
}

@end

旁注:我必须将帮助类作为子类SenTestCase才能访问断言宏。

辅助类的断言被忽略。任何想法为什么?如何在辅助类中使用断言?

4

1 回答 1

5

我今天遇到了同样的问题,并想出了一个对我有用的破解方法。戳入SenTestCase宏,我注意到他们在助手上调用 [self ...] 但没有触发断言。因此,将源类连接到帮助程序使其对我有用。对问题类的更改如下所示:

MainTests.h

#import <SenTestingKit/SenTestingKit.h>

@interface MainTests : SenTestCase

@end

MainTests.m

#import "MainTests.h"
#import "HelperClass.h"

@implementation MainTests

- (void)testExample {
    // Changed init call to pass self to helper
    HelperClass *helperClass = [[HelperClass alloc] initFrom:self];
    [helperClass fail];
}

@end

助手类.h

#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;

@property (nonatomic, strong) SenTestCase* from;

@end

助手类.m

#import "HelperClass.h"

@implementation HelperClass

@synthesize from;

- (id)initFrom:(SenTestCase *)elsewhere
{
    self = [super init];
    if (self) {
        self.from = elsewhere;
    }
    return self;
}

- (void)fail {
    STFail(@"This should fail");
}

// Override failWithException: to use the source test and not self
- (void) failWithException:(NSException *) anException {
    [self.from failWithException:anException];
}

@end

更高级的功能完全有可能需要额外的覆盖,但这对我有用。

于 2012-11-22T02:56:03.307 回答