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()
ような警告とビルド エラーが表示されます。
- 関数 'hasTotalNumberOfFeedItemCells' の暗黙の宣言は C99 では無効です
- 「int」から「id」への暗黙的な変換は、ARC では許可されていません
- 「hasTotalNumberOfFeedItemCells」のタイプが競合しています
どんな助けでも大歓迎です。