Domanda

I'm trying to write an XCTest (iOS7, XCode5) together with OCMock.

I've got a class which implements the CLLocationManagerDelegate protocol, and has a property which is an instance of a CLLocationManager. (I supply the instance of the CLLocationManager to the my initialiser method, so that I can inject it at runtime or test).

When testing the delegate class, I create a mock CLLocationManager.

In a test, I want to achieve something like this:

[[[[mockLocationManager stub] classMethod] andReturnValue:kCLAuthorizationStatusDenied] authorizationStatus];
result = [delegateUnderTest doMethod];
//Do asserts on result etc etc

The problem is, XCode is complaining about my code.

test.m:79:68: Implicit conversion of 'int' to 'NSValue *' is disallowed with ARC
test.m:79:68: Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSValue *'

kCLAuthorizationStatusDenied is an int I understand (as defined in a TypeDef). So, I can't use

[[[[mockLocationManager stub] classMethod] andReturn:kCLAuthorizationStatusDenied] authorizationStatus];

which would expect an object ('andReturn' is an 'id').

Any ideas?

È stato utile?

Soluzione

You need to box the value in an NSValue instance, and not pass the primitive value itself. For example:

[[[mockLocationManager stub] andReturnValue:@(kCLAuthorizationStatusDenied)] authorizationStatus];

The above makes use of the Objective-C literal syntax for NSNumbers. Also, I omitted the classMethod call above CLLocationManager doesn't have an instance method authorizationStatus.

More support for this can be found on the the OCMock website:

If the method returns a primitive type then andReturnValue: must be used with a value argument. It is not possible to pass primitive types directly.

That's also what the compiler error is telling you - that you are passing an int instead of an NSValue instance.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top