2014-01-15 6 views
2

iOS 응용 프로그램 테스트에 XCTestOCMock의 조합을 사용하고 있습니다. UITableViewController을 사용하여 몇 가지 테스트를 작성하고자하는 일련의 프로토 타입 셀을 표시합니다. 셀 자체가 스토리 보드에 있으므로 펜촉에서 인스턴스화 할 수 있다고 생각하지 않습니다.테스트 용 스토리 보드에서 프로토 타입 UITableViewCell을 인스턴스화하는 방법이 있습니까?

셀을 사용하여 인스턴스화하는 유일한 방법은 viewController입니까?

내가 사용하고있는 맞춤 셀 클래스에는 스토리 보드의 프로토 타입에 연결된 여러 개의 'IBOutlets'이 있습니다. 내가 [[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedAdCell"];를 사용하여 셀을 인스턴스화 시도했다하지만이 전무로 설정의 모든 속성과 셀을로드

@interface QRFeedAdCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UIImageView *mainImageView; 
@property (strong, nonatomic) IBOutlet UIImageView *blurredImageView; 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 
@property (strong, nonatomic) IBOutlet UIButton *someButton; 
@property (strong, nonatomic) IBOutlet UILabel *someLabel; 
@property (strong, nonatomic) IBOutlet UILabel *anotherLabel; 
@property (nonatomic) BOOL isBusy; 

@end 

: 셀 클래스는 다음과 같이 보인다.

는 또한 셀 등록을 시도했지만, 테스트는 실패 :

id mockTableView = [OCMockObject niceMockForClass:[UITableView class]]; 
[mockTableView registerClass:[QRFeedAdCell class] forCellReuseIdentifier:@"feedAdCell"]; 

QRFeedAdCell *cell = [mockTableView dequeueReusableCellWithIdentifier:@"feedAdCell"]; 

XCTAssertNotNil(cell, @""); 
XCTAssertNotNil(cell.mainImageView, @""); 

답변

4

할 수 없습니다.

테스트하는 유일한 방법은 스토리 보드를 인스턴스화 한 다음보기 컨트롤러를 누른 다음 셀 자체를 테스트하고 인터페이스 작성기에서 설정 한 속성을 테스트하는 것입니다.

+0

그게 내가 한거야, 나는 몇 가지 더 시도했지만 이것은 유일한 해결책 인 것 같다. 감사. – psobko

0

당신은 프로그래밍 방식으로 인스턴스화 할 수 있지만, 그런 다음 당신이 할 수있는 먼저

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"]; 

를 호출하여 등록해야 셀에 맞춤 스타일 지정 또는 이와 같은 스토리 보드에서 일반적으로하는 작업 스타일

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath]; 

     // create and add a label 
     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)]; 
     label.text = @"text"; 
     [cell addSubview:label]; 
     ... 
} 
+0

문제점은 스토리 보드에 설정되어있는 것을 테스트하고 싶습니다. 코드에 질문을 추가했지만 스토리 보드에서 IBOutlet에 액세스 할 수 없습니다. – psobko

+0

그래, 내가 제안한 것은 사용자 정의 셀의 모든 IBOutlet을 문제가되는 것으로 설정하는 것이었다. 수정 된 질문에 추가 정보가 주어지면 스토리 보드에서 셀을 다시 사용하려면 셀을 등록하거나 인스턴스화하지 않고'QRFeedAdCell * cell = [mockTableView dequeueReusableCellWithIdentifier : @ "feedAdCell"]; 스토리 보드에서 동일한 식별자 값을 설정하는 경우 –

+0

이렇게하면 작동하지 않습니다. Rodulf가 옳다고 생각합니다. 작동시킬 수있는 유일한 방법은 스토리 보드를 인스턴스화하는 것입니다. – psobko

0

이것은 기본적인 방식으로 작동합니다. 나는 그럭저럭 아직 달리기 위해 viewwillappear를 얻지 않았다.
_vc에서 .view를 호출하면 ViewdidLoad가 실행됩니다.

@property (nonatomic, strong) Edit_ProfileVC *vc; 
@property (nonatomic, strong) UIView *view; 
@property (nonatomic) NSMutableDictionary *params; 

UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    _vc = [mainSB instantiateViewControllerWithIdentifier:@"EditProfileVC"]; 
    _vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params]; 
    _view = _vc.view; 
    XCTAssertNotNil(_view, @"the view is not loading properly");