UITableViewCells를 모방하고 테스트하여 configureCell : forIndexPath가 올바르게 작동하는지 확인하려고합니다. isKindOfClass를 사용하여 작동하도록 설정할 수는 있지만 conformsToProtocol 만 사용할 수 있습니다. 이것은 모든 나의 uitableviewcells가 자신의 프로토콜을 가지고 필요로하지 않는 것입니다.UITableViewCell을 조롱하고 isKindOfClass를 사용합니다.
- (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 스텁] andReturnValue : OCMOCK_VALUE ((BOOL) {예})] isKindOfClass : [MyTableViewCell 클래스]]는 mockCell와] 기대와 그 중 하나가 작동하지 않습니다 . 이렇게 :
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에 대한 프로토콜을 사용해야합니까? 미리 감사드립니다.
configureCell 메서드 정의 란 무엇입니까? 그것이 전달 된 셀에 대해 MyTableViewCell *을 사용합니까? –
MyTableViewCell은 확실히 UITableViewCell의 하위 클래스입니까? –
configureCell 메서드 정의를 추가했으며 예 MyTableViewCell은 확실히 UITableViewCell의 하위 클래스입니다. xib을 사용하고 있으며 UITableViewCell을 확장하고 xib의 클래스 이름을 MyTableViewCell로 설정합니다. – user2828419