2

I tried stubbing AuthorizationStatus, but it alway returns kCLAuthorizationStatusResticted no matter what I do.

OCMStub([CLLocationManager authorizationStatus]).andReturn(kCLAuthorizationStatusAuthorizedAlways);

What did I do wrong?

iamarnold
  • 705
  • 1
  • 8
  • 22

1 Answers1

2

In general, you aren't going to stub invocations real classes or instances. In this case, you are stubbing an invocation on a class, when you should be stubbing an invocation on a mock. You will have to create a class mock and then stub the method invocation on the mock instead.

A simple example:

id locationManagerMock = OCMClassMock([CLLocationManager class]);
OCMStub([locationManagerMock authorizationStatus]).andReturn(kCLAuthorizationStatusAuthorizedAlways);

// Now this will pass!
XCTAssertEqualObjects([CLLocationManager authorizationStatus], kCLAuthorizationStatusAuthorizedAlways);

If you'd like to learn more, an almost identical example and a slightly more in-depth explanation of this can be found in OCMock's reference for Mocking class methods. The sections in the reference are a bit brief, but despite that it does a pretty good job of concisely explaining the framework and when you should use it.

Kyle Parent
  • 460
  • 3
  • 13