2013-02-25 2 views
2

I 설정 모의 관찰자 :왜 EXC_BAD_ACCESS을 가지고 센터를 nsnotificaiton의 원인이 OCMock 않는이 같은

id quartileObserverMock = [OCMockObject observerMock]; 
[[NSNotificationCenter defaultCenter] addMockObserver:quartileObserverMock 
               name:kVPAdPlayerDidReachQuartileNotification 
               object:self.adPlayer]; 

[[quartileObserverMock expect] 
         notificationWithName:kVPAdPlayerDidReachQuartileNotification 
            object:self.adPlayer 
            userInfo:@{@"quartile" : @(VPAdPlayerFirstQuartile), @"trackingEvent" : VPCreativeTrackingEventFirstQuartile}]; 

내 단위 테스트를 실행; 하지만 notificaiton 게시 할 때 가짜 EXC_BAD_ACCESS 오류 가져옵니다.

[[NSNotificationCenter defaultCenter] 
          postNotificationName:kVPAdPlayerDidReachQuartileNotification 
             object:self.adPlayer 
             userInfo:@{@"quartile" : @(quartile), @"trackingEvent" : trackingEvent}]; 

내 시험을 잘 매번 실행 observermock 코드를 주석 처리합니다.

코드를 다시 넣을 때 postNotiicaitonName : object : userInfo에 가짜 충돌이 발생합니다 (2.5 회마다 한 번).

누구나 아이디어가 있습니까?

+0

알림을 게시 할 때 개체 매개 변수는 self이지만 observer는 self.adPlayer에 대한 알림을 보려고합니다. 귀하의 질문에 오타가 있습니까? –

+0

예 - 그저 오타입니다. 나는 그것을 업데이트했다. – Infrid

답변

7

다음 샘플 코드를 참조하면 도움이됩니다. 예기치 않은 통지가 수신 될 때

  1. observerMock 항상 예외를 발생 : 그것은

    - (void)postNotificationwithName:(NSString *)notifName userInfo:(NSDictionary *)userInfo 
    { 
        [[NSNotificationCenter defaultCenter] postNotificationName:notifName object:self userInfo:userInfo]; 
    } 
    

    주의 사항 나

    - (void)test__postNotificationwithName__withUserInfo 
    { 
        id observerMock = [OCMockObject observerMock]; 
        [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:@"NewNotification" object:nil]; 
    
        NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"2",@"2", nil]; 
        [[observerMock expect] notificationWithName:@"NewNotification" object:[OCMArg any] userInfo:userInfo]; 
    
        NotificationClass *sut = [[NotificationClass alloc] init]; 
        [sut postNotificationwithName:@"NewNotification" userInfo:userInfo]; 
    
        [[NSNotificationCenter defaultCenter] removeObserver:observerMock]; 
        [observerMock verify]; 
    } 
    

    내 게시 알림 방법 일했다.

  2. 테스트 케이스 끝의 NSNotificationCenter에서 조롱 한 관찰자를 제거하십시오.

    1) (난 그냥 내 자신의 코드 오늘 그 둘을 해결하기 때문에, 내가 아는)

0

는 이런 일이 될 수있는 이유는 몇 가지 이유가 있습니다 이전 테스트에서 모의 ​​관찰자는 없었다 removed

2) 이전 테스트의 모의가없는 인스턴스 객체가 동일한 알림을 관찰하고 있지만 그 객체는 사용되지 않게되었습니다. 내 경우에는 setUp 메쏘드의 인스턴스 객체가 알림을 듣고 있었지만, 할당 해제되었을 때 NSNotificationCenter의 옵서버 목록에서 제거되지 않았습니다.

용액 범위에 따라

[[NSNotificationCenter defaultCenter] removeObserver:name:object:] 

를 사용하는 두 경우

1)의 끝에 NSNotificationCenter, 2) tearDown 방법, 또는 3) 관찰 모든 클래스의 dealloc에서에게 테스트 사례 (@PrasadDevadiga 언급 한대로)