Features
Class mocks id mock = [OCMockObject mockForClass:[SomeClass class]]; Creates a mock object that can be used as if it were an instance of |
|
Expectations and verification [[mock expect] someMethod:someArgument]; Tells the mock object that [mock verify]; The verify method will raise an exception if the expected method has not been invoked. In some cases the expected method will only be called when the run loop is active. For these cases it is possible to delay the verification for a while. [mock verifyWithDelay:aDelay]; Note that |
|
Stubs [[[mock stub] andReturn:aValue] someMethod:someArgument]; Tells the mock object that when If the method returns a primitive type then [[[mock stub] andReturnValue:@YES] aMethodReturnABoolean:someArgument]; Values can also be returned in pass-by-reference arguments: [[mock stub] someMethodWithReferenceArgument:[OCMArg setTo:anObject]]; [[mock stub] someMethodWithReferenceArgument:[OCMArg setToValue:OCMOCK_VALUE((int){aValue})]]; In this case the mock object will set the reference that is passed to the method to anObject and aValue. The mock object can also throw an exception or post a notification when a method is called: [[[mock stub] andThrow:anException] someMethod:someArgument]; [[[mock stub] andPost:aNotification] someMethod:someArgument]; In fact, the notification can be posted in addition to returning a value: [[[[mock stub] andPost:aNotification] andReturn:aValue] someMethod:someArgument]; The mock can delegate the handling of an invocation to a completely different method: [[[mock stub] andCall:@selector(aMethod:) onObject:anObject] someMethod:someArgument]; In this case the mock object will call If Objective-C blocks are available a block can be used to handle the invocation and set up a return value: void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) { /* code that reads and modifies the invocation object */ }; [[[mock stub] andDo:theBlock] someMethod:[OCMArg any]]; If using a partial mock it is possible to forward the method to the implementation in the real object, which can be useful to simply check that a method was called: [[[mock expect] andForwardToRealObject] someMethod]; Note that it is possible to use |
|
Class methods [[[mock stub] andReturn:aValue] someClassMethod]; Tells the mock object that when As with partial mocks it is possible to use [[[mock expect] andForwardToRealObject] someClassMethod]; In cases where a class method should be stubbed but the class also has an instance method with the same name as the class method, the intent to mock the class method must be made explicit: [[[[mock stub] classMethod] andReturn:aValue] aMethod]; The class can be returned to its original state, i.e. all stubs will be removed: [mock stopMocking]; This is only necessary if the original state must be restored before the end of the test. The mock automatically calls Note: If the mock object that added a stubbed class method is not deallocated the stubbed method will persist across tests. If multiple mock objects manipulate the same class at the same time the behaviour is undefined. |
|
Argument constraints [[mock expect] someMethod:[OCMArg any]]; Tells the mock object that Pointers and selectors require special treatment: [[mock expect] someMethodWithPointerArgument:[OCMArg anyPointer]]; [[mock expect] someMethodWithSelectorArgument:[OCMArg anySelector]]; Arguments that are neither objects nor pointers or selectors cannot be ignored using an any placeholder (for details see this forum thread). It is possible, though, to tell the mock to ignore all non-object arguments in an invocation: [[[mock expect] ignoringNonObjectArgs] someMethodWithIntArgument:0]; In this case the mock will accept any invocation of Other constraints available for object arguments are: [[mock expect] someMethod:[OCMArg isNil]]; [[mock expect] someMethod:[OCMArg isNotNil]]; [[mock expect] someMethod:[OCMArg isNotEqual:aValue]]; [[mock expect] someMethod:[OCMArg checkWithSelector:aSelector onObject:anObject]]; The last constraint will, when the mock object receives If Objective-C blocks are available it is possible to check the argument with a block as follows: [[mock expect] someMethod:[OCMArg checkWithBlock:^BOOL(id value) { /* return YES if value is ok */ }]]; Last but not least it is also possible to use Hamcrest matchers like this: [[mock expect] someMethod:startsWith(@"foo")]; Note that this will only work when the Hamcrest framework is explicitly linked by the unit test bundle. |
|
Nice mocks/failing fast When a method is called on a mock object that has not been set up with either expect or stub the mock object will raise an exception. This fail-fast mode can be turned off by creating a "nice" mock: id mock = [OCMockObject niceMockForClass:[SomeClass class]]; While nice mocks will simply ignore all unexpected methods it is possible to disallow specific methods: [[mock reject] someMethod]; Note that in fail-fast mode, if the exception is ignored, it will be rethrown when verify is called. This makes it possible to ensure that unwanted invocations from notifications etc. can be detected. |
|
Protocol mocks id aMock = [OCMockObject mockForProtocol:@protocol(SomeProtocol)]; Creates a mock object that can be used as if it were an instance of an object that implements |
|
Partial mocks id aMock = [OCMockObject partialMockForObject:anObject]; Creates a mock object that can be used in the same way as The real object can be returned to its original state, i.e. all stubs will be removed: [aMock stopMocking]; This is only necessary if the original state must be restored before the end of the test. The partial mock automatically calls Note that currently partial mocks cannot be created for instances of toll-free bridged classes, e.g. NSString. |
|
Observer mocks id aMock = [OCMockObject observerMock]; Creates a mock object that can be used to observe notifications. The mock must be registered in order to receive notifications: [notificatonCenter addMockObserver:aMock name:SomeNotification object:nil]; Expectations can then be set up as follows: [[mock expect] notificationWithName:SomeNotification object:[OCMArg any]]; Note that currently there is no "nice" mode for observer mocks, they will always raise an exception when an unexpected notification is received. |
|
Instance-based method swizzling In a nutshell, Method Swizzling describes the replacement of a method implementation with a different implementation at runtime. Using partial mocks and the id mock = [OCMockObject partialMockForObject:anObject]; [[[mock stub] andCall:@selector(differentMethod:) onObject:differentObject] someMethod:[OCMArg any]]; After these two lines, when |