Class mocks create objects that are pure mocks of an instance of a class.
Partial mocks take an instance of class a so you can drown out any of its methods.
Suppose I have these classes:
@interface Foo : NSObject - (void)doX; @end @implementation - (void)doX { NSLog(@"X"); } @end @interface Bar : NSObject - (void)doA:(Foo *)foo; - (void)doB; @end @implementation Bar - (void)doA:(Foo *)foo { NSLog(@"A"); [foo doX]; [self doB]; } - (void)doB { NSLog(@"B"); } @end
I'm interested in the testing method Bar doA: . I expect it to call doX for the Foo object and then call its own doB method. I would implement this using a Foo class layout and a partial bar layout.
- (void)test_doA_shouldCall_doX_and_doB { id objectUnderTest = [OCMockObject partialMockForObject:[Bar new]]; id fooMock = [OCMockObject mockForClass:Foo.class]; [[fooMock expect] doX]; [[objectUnderTest expect] doB];
You see here that my partial layout allowed me to invoke a real method that I wanted to test while ridiculing an internal call to another of its instance methods. However, since I do not need any real Foo functions, I used the mock class.
source share