我正在尝试编写自己的HCMatcher
,在处理对象集合时,我可以使用它只是一些断言。
目前我的测试方法是这样做的:
__block int totalNumberOfCells = 0;
[configurations enumerateObjectsUsingBlock:^(Layout *layout, NSUInteger idx, BOOL *stop) {
totalNumberOfCells += [layout numberOfCells];
}];
assertThatInt(totalNumberOfCells, is(equalToInt(3)));
我将在许多地方做这种断言,所以我想把它简化为类似的东西:
assertThat(configurations, hasTotalNumberOfFeedItemCells(3));
这是我尝试创建自己的OCHamcrest匹配器:
@interface HasTotalNumberOfFeedItemCells : HCBaseMatcher {
NSInteger correctNumberOfCells;
}
- (id)initWithCorrectNumberOfCells:(NSInteger)num;
@end
OBJC_EXPORT id <HCMatcher> hasTotalNumberOfFeedItemCells(int num);
@implementation HasTotalNumberOfFeedItemCells
- (id)initWithCorrectNumberOfCells:(NSInteger)num {
self = [super init];
if (self) {
correctNumberOfCells = num;
}
return self;
}
- (BOOL)matches:(id)item {
__block int totalNumberOfCells = 0;
[item enumerateObjectsUsingBlock:^(Layout *layout, NSUInteger idx, BOOL *stop) {
totalNumberOfCells += [layout numberOfCells];
}];
return totalNumberOfCells == correctNumberOfCells;
}
- (void)describeTo:(id <HCDescription>)description {
[description appendText:@"ZOMG"];
}
@end
id <HCMatcher> hasTotalNumberOfFeedItemCells(int num){
return [[HasTotalNumberOfFeedItemCells alloc] initWithCorrectNumberOfCells:num];
}
当我尝试使用hasTotalNumberOfFeedItemCells()
函数时,我收到警告并构建错误说:
非常感谢任何帮助。