fab*_*abb 1 unit-testing assert ios sentestingkit
我有一个测试用例和一个帮助类.在helper类中,我想在这里使用assert:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
@interface MainTests : SenTestCase
@end
Run Code Online (Sandbox Code Playgroud)
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
@implementation MainTests
- (void)testExample {
HelperClass *helperClass = [[HelperClass alloc] init];
[helperClass fail];
}
@end
Run Code Online (Sandbox Code Playgroud)
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
@interface HelperClass : SenTestCase
- (void)fail;
@end
Run Code Online (Sandbox Code Playgroud)
HelperClass.m
#import "HelperClass.h"
@implementation HelperClass
- (void)fail {
STFail(@"This should fail");
}
@end
Run Code Online (Sandbox Code Playgroud)
旁注:我必须使辅助类成为子类,SenTestCase才能访问断言宏.
来自辅助类的断言被忽略.有什么想法吗?如何在辅助类中使用断言?
我今天遇到了同样的问题,并提出了一个适用于我的目的的黑客攻击.进入SenTestCase宏,我注意到他们在帮助器上调用[self ...]但没有触发断言.因此,将源类连接到帮助程序让它为我工作.您的问题类的更改如下所示:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
@interface MainTests : SenTestCase
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
@interface HelperClass : SenTestCase
- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;
@property (nonatomic, strong) SenTestCase* from;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
完全有可能需要额外的覆盖才能获得更高级的功能,但这对我来说非常有用.
| 归档时间: |
|
| 查看次数: |
275 次 |
| 最近记录: |