我正在尝试模拟和测试 UITableViewCells 以确保我的 configureCell:forIndexPath 工作正常,但我无法使用 isKindOfClass 使其工作,而只能使用符合 ToProtocol。这将要求我所有的 uitableviewcells 都有自己的协议,并且似乎不需要。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FeedObj *item = [_feedElements objectAtIndex:indexPath.row];
if( item.obj_type == FeedObjTypeFriendAdd ) {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath]
return cell;
} else if( item.obj_type = FeedObjTypeSomeOtherType ) {
// do another cell
}
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
// only enters conditional in test if I do [cell conformsToProtocol:@protocol(SomeIndividualProtocolForEachTableViewcell)]
if( [cell isKindOfClass:[MyTableViewCell class]] ) {
// do the configuring
FeedObj *item = [_streamElements objectAtIndex:indexPath.row];
NSString *firstName = [item.obj_data objectForKey:@"first_name"];
NSString *lastName = [item.obj_data objectForKey:@"last_name"];
NSString *name = [NSString stringWithFormat:@"%@ %@.", firstName, [lastName substringToIndex:1]];
NSString *text = [NSString stringWithFormat:@"%@ has joined", name];
[((MyTableViewCell *)cell).messageLabel setText:text];
} else if( [cell isKindOfClass[SomeOtherTableView class]] ) {
// do other config
}
}
@implementation SampleTests
- (void)setUp
{
_controller = [[MySampleViewController alloc] init];
_tableViewMock = [OCMockObject niceMockForClass:[UITableView class]];
[_tableViewMock registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:MyTableViewCellIdentifier];
}
- (void)testFriendAddCell
{
FeedObj *friendAdd = [[FeedObj alloc] init];
friendAdd.obj_type = FeedObjTypeFriendAdd;
friendAdd.obj_data = [NSMutableDictionary dictionaryWithDictionary:@{ @"first_name" : @"firstname", @"last_name" : @"lastname" }];
_mockStreamElements = [NSMutableArray arrayWithObject:friendAdd];
[_controller setValue:_mockStreamElements forKey:@"_feedElements"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[[[_tableViewMock expect] andReturn:[[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] lastObject]] dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
MyTableViewCell *cell = (MyTableViewCell *)[_controller tableView:_tableViewMock cellForRowAtIndexPath:indexPath];
STAssertNotNil( cell, @"should not be nil" );
STAssertTrue( [cell.messageLabel.text isEqualToString:@"firstname l. has joined"], @"should be equal" );
[_tableViewMock verify];
}
@end
我也尝试过使用 mockCell 期望做 [[[mockCell stub] andReturnValue:OCMOCK_VALUE((BOOL) {YES})] isKindOfClass:[MyTableViewCell class]]] 并且它也不起作用。像这样:
id mockCell = [OCMockObject partialMockForObject:[[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] lastObject]];
[[[mockCell stub] andReturnValue:OCMOCK_VALUE((BOOL) {YES})] isKindOfClass:[OCMConstraint isKindOfClass:[MyTableViewCell class]]];
[[[_tableViewMock expect] andReturn:mockCell] dequeueReusableCellWithIdentifier:MyTableViewCellIdentifier forIndexPath:indexPath];
我什至尝试使用http://blog.carbonfive.com/2009/02/17/custom-constraints-for-ocmock/中列出的 OCMConstraint 。
无论如何要这样做还是我必须为每个 tableviewcell 使用协议?提前致谢