Note that there are some explanatory texts on larger screens.

plurals
  1. POCLLocationManagerDelegate mocking on users denial
    primarykey
    data
    text
    <p><strong>-- Final Edit:</strong> More accurate question <a href="https://stackoverflow.com/questions/20376000/unit-test-cllocationmanager-implementation">Unit Test CLLocationManager implementation</a></p> <p>I'm trying to mock CLLocationManagerDelegate to handle users denial about being geolocated</p> <pre><code>id locationManager = [OCMockObject niceMockForClass:[CLLocationManager class]]; [[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus]; id delegateTarget = [OCMockObject niceMockForProtocol:@protocol(CLLocationManagerDelegate)]; [[delegateTarget expect] locationManager:locationManager didFailWithError:[OCMArg any]]; [locationManager setDelegate:delegateTarget]; [locationManager startUpdatingLocation]; [delegateTarget verify]; </code></pre> <p>However this code doesn't work, I keep getting this error:</p> <blockquote> <p>Name: NSInternalInconsistencyException File: Unknown Line: Unknown Reason: OCMockObject[CLLocationManagerDelegate]: expected method was not invoked: locationManager:OCMockObject[CLLocationManager] didFailWithError:</p> </blockquote> <p>How can I be sure that the CLLocationManagerDelegate method locationManager:didFailWithError: is getting called?</p> <p>Edit: If I use partialMock instead of niceMock for my CLLocationManager instance:</p> <pre><code>CLLocationManager *trueLocationManager = [[CLLocationManager alloc] init]; id locationManager = [OCMockObject partialMockForObject:trueLocationManager]; [[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus]; id delegateTarget = [OCMockObject mockForProtocol:@protocol(CLLocationManagerDelegate)]; [[delegateTarget expect] locationManager:trueLocationManager didFailWithError:[OCMArg any]]; [locationManager setDelegate:delegateTarget]; [locationManager startUpdatingLocation]; [delegateTarget verify]; </code></pre> <p>I get this:</p> <blockquote> <p>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[CLLocationManagerDelegate]: unexpected method invoked: locationManager: didChangeAuthorizationStatus:2 expected: locationManager: didFailWithError:'</p> </blockquote> <p>So I thought I got it and added the code below before my delegateTarget expectation of didFailWithError</p> <pre><code>[[delegateTarget expect] locationManager:trueLocationManager didChangeAuthorizationStatus:2]; // kCLAuthorizationStatusDenied = 2 </code></pre> <p>And the result is:</p> <blockquote> <p>Name: NSInternalInconsistencyException File: Unknown Line: Unknown Reason: OCMockObject[CLLocationManagerDelegate] : 2 expected methods were not invoked: locationManager: didChangeAuthorizationStatus:2 locationManager: didFailWithError:</p> </blockquote> <p>Really don't know what I'm doing wrong...</p> <p>Edit2: for Ilea Cristian </p> <pre><code>CLLocationManager *trueLocationManager = [[CLLocationManager alloc] init]; id locationManager = [OCMockObject partialMockForObject:trueLocationManager]; [[[locationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus]; id delegateTarget = [OCMockObject mockForProtocol:@protocol(CLLocationManagerDelegate)]; [[delegateTarget expect] locationManager:locationManager didChangeAuthorizationStatus:2]; // kCLAuthorizationStatusDenied = 2 [[delegateTarget expect] locationManager:locationManager didFailWithError:[OCMArg any]]; [locationManager setDelegate:delegateTarget]; [locationManager startUpdatingLocation]; [delegateTarget verify]; </code></pre> <p>I got this:</p> <blockquote> <p>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[CLLocationManagerDelegate]: unexpected method invoked: locationManager: didChangeAuthorizationStatus:2 expected: locationManager:OCPartialMockObject[CLLocationManager] didChangeAuthorizationStatus:2 expected: locationManager:OCPartialMockObject[CLLocationManager] didFailWithError:'</p> </blockquote> <p>Looks like locationManager used in the delegate callback is the trueLocationManager not the locationManager (partialMock)</p> <p><strong>-- Final Edit:</strong> More accurate question <a href="https://stackoverflow.com/questions/20376000/unit-test-cllocationmanager-implementation">Unit Test CLLocationManager implementation</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload