Here are some examples I copied from the README files of the webs site for learning. Go here to see a latest documentation
https://github.com/jonreid/OCMockito
#import <XCTest/XCTest.h> #define HC_SHORTHAND #import <OCHamcrestIOS/OCHamcrestIOS.h> #define MOCKITO_SHORTHAND #import <OCMockitoIOS/OCMockitoIOS.h> #import <XCTest/XCTest.h> #import "WeatherService.h" @interface Examples : XCTestCase @end @implementation Examples - (void)testMocking { // mock creation NSMutableArray *mockArray = mock([NSMutableArray class]); // using mock object [mockArray addObject:@"one"]; [mockArray removeAllObjects]; // verification [verify(mockArray) addObject:@"one"]; // verify -> MKTVerify if 'Bad Receiver type 'int' [verify(mockArray) removeAllObjects]; // Once created, the mock will remember all interactions. // Then you can selectively verify whatever interactions you are interested in later. } - (void)testStubbing { // mock creation NSArray *mockArray = mock([NSArray class]); // stubbing [given([mockArray objectAtIndex:0]) willReturn:@"first"]; [given([mockArray objectAtIndex:1]) willThrow:[NSException exceptionWithName:@"name" reason:@"reason" userInfo:nil]]; // following prints "first" NSLog(@"%@", [mockArray objectAtIndex:0]); // follows throws exception // NSLog(@"%@", [mockArray objectAtIndex:1]); // following prints "(null)" because objectAtIndex:999 was not stubbed NSLog(@"%@", [mockArray objectAtIndex:999]); } - (void)testProtocol { id <WeatherServiceDelegate> delegate = mockProtocol(@protocol(WeatherServiceDelegate)); } - (void)testStubMethodsThatReturnPrimitives { NSMutableArray *mockArray = mock([NSMutableArray class]); [given([mockArray count]) willReturn:@3]; } - (void)testArgumentMatchers { // OCMockito normally verifies based on exact equality. But you can also do this. // mock creation NSMutableArray *mockArray = mock([NSMutableArray class]); // using mock object [mockArray removeObject:@"This is a test"]; // verification [verify(mockArray) removeObject:startsWith(@"This is")]; } - (void)testSpecifyMatchersForNonObjectArguments { // to stub a methd that takes a non-object arg, invoke method with dummy then can withMatcher:forArgument NSMutableArray *mockArray = mock([NSMutableArray class]); [[given([mockArray objectAtIndex:0]) withMatcher:anything() forArgument:0] willReturn:@"foo"]; [[given([mockArray objectAtIndex:0]) withMatcher:anything()] willReturn:@"foo"]; } - (void)testExactNumberOfInnovations { NSMutableArray *mockArray = mock([NSMutableArray class]); // using mock [mockArray addObject:@"once"]; [mockArray addObject:@"twice"]; [mockArray addObject:@"twice"]; // the following two verifications work exactly the same [verify(mockArray) addObject:@"once"]; [verifyCount(mockArray, times(1)) addObject:@"once"]; // verify exact number of invocations [verifyCount(mockArray, times(2)) addObject:@"twice"]; [verifyCount(mockArray, times(0)) addObject:@"three times"]; // verify using never(), which is an alias for times(0) [verifyCount(mockArray, never()) addObject:@"never happened"]; // verify using atLeast()/atMost() [verifyCount(mockArray, atLeastOnce()) addObject:@"once"]; [verifyCount(mockArray, atLeast(2)) addObject:@"twice"]; // [verifyCount(mockArray, atMost(5)) addObject:@"at most five times"]; } - (void)testCanStubConsecutiveCalls { // is you ever need to stub consequtive calls together you can do it like this // [[given([mockObject someMethod:@"some arg"]) // willThrow:[NSException exceptionWithName:@"name" reason:@"reason" userInfo:nil]] // willReturn:@"foo"]; // First call: throws exception // [mockObject someMethod:@"some arg"]; // Second call: prints "foo" // NSLog(@"%@", [mockObject someMethod:@"some arg"]); // Any consecutive call: prints "foo" as well. (Last stubbing wins.) // NSLog(@"%@", [mockObject someMethod:@"some arg"]); } @end // Links // Bad Receiver Type https://github.com/jonreid/OCMockito/issues/65 //
Leave a Reply