我有UITableView
它有代表和分離的數據源。 DataSource是NSObject
- ArrayDataSource
類的子類。我應該如何測試塊是否已經在這個類中引發?
#import <Foundation/Foundation.h>
typedef void (^ConfigureCellBlock)(id cell, id object);
@interface ArrayDataSource : NSObject <UITableViewDataSource>
@property (nonatomic, readonly) NSArray *items;
@property (nonatomic, readonly) NSString *cellIdentifier;
- initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier configureCellBlock:(ConfigureCellBlock)block;
- (void)setItems:(NSArray *)items;
- (void)setCellIdentifier:(NSString *)cellIdentifier;
- (void)setConfigureCellBlock:(ConfigureCellBlock)block;
@end
#import "ArrayDataSource.h"
@interface ArrayDataSource()
@property (nonatomic, copy) ConfigureCellBlock configureCellBlock;
@end
@implementation ArrayDataSource {
NSArray *_items;
NSString *_cellIdentifier;
}
#pragma mark - External
- (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier configureCellBlock:(ConfigureCellBlock)block {
self = [super init];
if (self) {
_items = items;
_cellIdentifier = cellIdentifier;
_configureCellBlock = [block copy];
}
return self;
}
- (void)setItems:(NSArray *)items {
_items = items;
}
- (void)setCellIdentifier:(NSString *)cellIdentifier {
_cellIdentifier = cellIdentifier;
}
- (void)setConfigureCellBlock:(ConfigureCellBlock)block {
_configureCellBlock = block;
}
#pragma mark - Private
- (id)_itemAtIndexPath:(NSIndexPath *)indexPath {
return _items[indexPath.row];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:_cellIdentifier forIndexPath:indexPath];
id item = [self _itemAtIndexPath:indexPath];
_configureCellBlock(cell, item);
return cell;
}
#pragma mark - Accessors
- (NSArray *)items {
return _items;
}
- (NSString *)cellIdentifier {
return _cellIdentifier;
}
@end
現在我要測試是否ConfigureCellBlock
已tableView:cellForRowAtIndexPath:
被稱爲使用正確的參數。
我的測試現在看起來像下面但它不能正常工作,因爲它傳遞和塊並不是所有的調用。我的測試班是_tableView
的代表。
#pragma mark - UITableView tests + Delegate + DataSource
- (void)testDataSource {
UITableView *_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
[dataSource setItems:@[@"A"]];
[dataSource setConfigureCellBlock:^(UITableViewCell *cell, NSString *object) {
XCTAssertEqualObjects(object, @"B", @"");
}];
id mockDataSource = [OCMockObject partialMockForObject:dataSource];
SEL selector = NSSelectorFromString(@"_itemAtIndexPath:");
[[[mockDataSource stub] andReturn:dataSource.items[0]] methodForSelector:selector];
_tableView.dataSource = mockDataSource;
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
id mockTableView = [OCMockObject partialMockForObject:_tableView];
[[[mockTableView stub] andReturn:cell] dequeueReusableCellWithIdentifier:OCMOCK_ANY forIndexPath:indexPath];
[mockTableView reloadData];
}
我應該如何正確地測試它?
預先感謝您。
這幾乎肯定不會解決您的問題,但不應該你的塊設置複製塊(就像你在構造函數中做的那樣)? – sammyd
@sammyd這個副本在構造函數中是不必要的,因爲屬性是副本。但是,謝謝你。 –
構造函數*中的'[copy]'是必需的,因爲您將塊分配給支持該屬性的ivar。屬性聲明中的'(copy)'指令只適用於合成的setter。然而,在你的代碼中,你已經覆蓋了setter,並且沒有使用copy,所以即使你使用'self.configureCellBlock = block',它也不會複製。 – sammyd