IOS OCMock partial vs class mock

I am learning OCMock for testing iOS. What is the difference between a “cool layout” and a “partial layout”, and when should you use one against the other?

http://ocmock.org/features/

+6
source share
1 answer

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]; // Make the call [objectUnderTest doA:fooMock]; [objectUnderTest verify]; [fooMock verify]; } 

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.

+12
source

Source: https://habr.com/ru/post/951945/


All Articles