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

HelperClass.h

#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

- (void)fail;

@end

HelperClass.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

HelperClass.h

#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

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

@property (nonatomic, strong) SenTestCase* from;

@end

HelperClass.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 に答える