Reputation: 11745
I have a test case and a helper class. In the helper class I want to use asserts too like here:
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
Sidenote: I had to make the helper class a subclass from SenTestCase
to being able to access the assertion macros.
The assertion from the helper class is ignored. Any ideas why? How can I use assertions in helper classes?
Upvotes: 1
Views: 291
Reputation: 1951
I had this same problem today and came up with a hack that worked for my purposes. Poking into the SenTestCase
macros, I noticed that they call [self ...] on the helper but didn't trigger the asserts. So, wiring up the source class to the helper got it working for me. Changes to your question classes would look like:
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
It is entirely possible that additional overrides are needed for more advanced functionality, but this did the trick for me.
Upvotes: 5