Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Compare: Mocks and Stubs

Showing with 24 additions and 0 deletions.
  1. +24 −0
@@ -4,6 +4,7 @@
Mocks are objects that imitate a class, or look like they conform to a protocol. They let you focus on interaction behavior between objects before full implementations exist, and to isolate objects from dependencies that may be too expensive or unreliable to use when running specs.
id carMock = [Car mock];
[ [carMock should] beMemberOfClass:[Car class]];
[[carMock should] receive:@selector(currentGear) andReturn:theValue(3)];
@@ -19,6 +20,7 @@ Mocks are objects that imitate a class, or look like they conform to a protocol.
id flyerNullMock = [KWMock nullMockForProtocol:@protocol(FlyingMachine)];
[flyerNullMock takeOff];
### Null Mocks
@@ -71,6 +73,7 @@ Stubbing message patterns:
id cruiser = [Cruiser cruiser];
[[cruiser stubAndReturn:theValue(42.0f)] energyLevelInWarpCore:7];
float energyLevel = [cruiser energyLevelInWarpCore:7];
@@ -82,6 +85,27 @@ Example:
id mock = [Animal mock];
[mock stub:@selector(species) andReturn:@"P. tigris"];
[[mock.species should] equal:@"P. tigris"];
+### Capturing Arguments
+At times, you may want to capture an argument that is passed to mock object. For instance, the argument may be an object that did not implement `isEqual:` and it must be captured in order to be verified properly. Alternatively, and the most likely case, is that the mock receives a message where one of the arguments is a [block]( and it must be captured and invoked in order to verify the behavior of that block.
+ id robotMock = [KWMock nullMockForClass:[Robot class]];
+ KWCaptureSpy *spy = [robotMock captureArgument:@selector(speak:afterDelay:whenDone:) atIndex:2];
+ [[[robotMock should] receive] speak:@"Goodbye"];
+ [robotMock speak:@"Hello" afterDelay:2 whenDone:^{
+ [robotMock speak:@"Goodbye"];
+ }];
+ void (^block)(void) = spy.argument;
+ block();
### Advanced Stubs and Memory Management
Something went wrong with that request. Please try again.