2010-05-04 3 views
0

역 지오 코딩을 사용하는 방법을 테스트하고 싶습니다. 내가 뭘하고 싶은 것입니다 : OCmock 및 MKReverseGeocoder

  • 는 init 메소드에

  • 호출 내가 원하는 방법에서 지오 코더를 지오 코더를 만들어 내 컨트롤러

  • 의 속성으로 지오 코더를 설정 테스트

  • 내 테스트에서 모의로 지오 코더를 교체

,

문제는 내가 단지 생성자 메서드에서 설정 수 있으며, MKReverseGeocoder 재산을 좌표 만 읽기 :

[[MKReverseGeocoder alloc] initWithCoordinate:coord] 

물론 좌표가 내가 테스트 할 방법에서만 사용할 수 있습니다

..

MKReverseGeocoder 클래스를 조롱하는 방법을 아는 사람이 있습니까?

미리 감사드립니다. Vincent.

답변

0

체크 아웃 Matt Gallagher's great article on unit testing Cocoa applications을 확인하십시오. 그는 테스트 시간에 인스턴스를 대체 할 수 있도록 NSObject에 범주 확장을 제공합니다. 나는 비슷한 것을하기 위해 그것을 사용했다. 귀하의 테스트는 다음과 같이 보일 것입니다 :

#import "NSObject+SupersequentImplementation.h" 

id mockGeocoder = nil; 

@implementation MKReverseGeocoder (UnitTests) 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate { 
    if (mockGeocoder) { 
     // make sure the mock returns the coordinate passed in 
     [[[mockGeocoder stub] andReturn:coordinate] coordinate]; 
     return mockGeocoder; 
    } 
    return invokeSupersequent(coordinate); 
} 

@end 

... 

-(void) testSomething { 
    mockGeocoder = [OCMockObject mockForClass:[MKReverseGeocoder class]]; 
    [[mockGeocoder expect] start]; 

    // code under test 
    [myObject geocodeSomething]; 

    [mockGeocoder verify]; 
    // clean up 
    mockGeocoder = nil; 
} 
관련 문제