iamarnold
iamarnold

Reputation: 725

Can't Stub [CLLocationManager AuthorizationStatus] with OCMock

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

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

What did I do wrong?

Upvotes: 2

Views: 472

Answers (1)

Kyle Parent
Kyle Parent

Reputation: 460

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.

Upvotes: 2

Related Questions