I got tired of not understanding how OCMockito worked and created the following dirt simple examples to show me how.

SimpleTest.m

#import <XCTest/XCTest.h>

#import "Handler.h"
#import "Transport.h"

#import <OCHamcrestIOS/OCHamcrestIOS.h>
#import <OCMockitoIOS/OCMockitoIOS.h>


@interface HandlerTest : XCTestCase
@property(nonatomic, strong) Handler *sut;
@property(nonatomic, strong) Transport *mockTransport;
@end

@implementation HandlerTest

- (void)setUp
{
    [super setUp];
    self.mockTransport = mock([Transport class]);
    self.sut = [[Handler alloc] initWithTransport:self.mockTransport];
}

- (void)testSimpleVerify
{
    [self.sut simpleMethod];
    [verify(self.mockTransport) foo];
}

- (void)testSimpleVerifyWithSpecificArgument
{
    [self.sut simpleMethodWithArgument:@"bar"];
    [verify(self.mockTransport) fooWithArgument:@"bar"];
}

- (void)testSimpleStub
{
    [given([self.mockTransport connected]) willReturnBool:YES];
    XCTAssert([self.sut isTransportConnected]);
}

- (void)testMethodWithCallback
{
    [self.sut simpleMethodWithCallback];
    [verify(self.mockTransport) fooWithCallback:anything()];
}

- (void)testMethodWithCallbackAndParameters
{
    [self.sut simpleMethodWithCallbackAndParameters];

    HCArgumentCaptor *argument = [HCArgumentCaptor new];
    [verify(self.mockTransport) fooWithCallbackAndParameters:(id)argument];

    void (^callback)(NSArray*, BOOL success) = argument.value;

    NSArray* result = [NSArray new];
    callback(result, YES);

    assertThat(self.sut.model, equalTo(result));
}

@end

Handler.h

#import <Foundation/Foundation.h>
#import "Transport.h"

@interface Handler : NSObject

- (instancetype)initWithTransport:(Transport *)transport;

@property (nonatomic, strong) NSArray *model;

- (void)simpleMethod;
- (void)simpleMethodWithArgument:(NSString *)argument1;
- (void)simpleMethodWithCallback;
- (void)simpleMethodWithCallbackAndParameters;
- (BOOL)isTransportConnected;

@end

Handler.m

#import "Handler.h"
#import "Transport.h"

@interface Handler()
@property (nonatomic, strong) Transport *transport;
@end

@implementation Handler

- (instancetype)initWithTransport:(Transport *)transport
{
    self = [super init];
    if (self) {
        _transport = transport;
    }
    return self;
}

- (void)simpleMethod
{
    [self.transport foo];
}

- (void)simpleMethodWithArgument:(NSString *)argument1
{
    [self.transport fooWithArgument:argument1];
}

- (BOOL)isTransportConnected
{
    return self.transport.connected;
}

- (void)simpleMethodWithCallback
{
    [self.transport fooWithCallback:^(id data, NSError *error) {

    }];
}

- (void)simpleMethodWithCallbackAndParameters
{
    [self.transport fooWithCallbackAndParameters:^(NSArray *result, BOOL success) {
        if (success) {
            self.model = result;
        }
    }];
}

@end

Transport.h

#import <Foundation/Foundation.h>

@interface Transport : NSObject
@property (nonatomic, assign) BOOL connected;
- (void)foo;
- (void)fooWithArgument:(NSString *)argument1;
- (void)fooWithCallback:(void(^)(id data, NSError *error))block;
- (void)fooWithCallbackAndParameters:(void(^)(NSArray *result, BOOL success))block;
@end

Transport.m

#import "Transport.h"

@implementation Transport
- (void)foo {}
- (void)fooWithArgument:(NSString *)argument1 {}
- (void)fooWithCallback:(void(^)(id data, NSError *error))block {}
- (void)fooWithCallbackAndParameters:(void(^)(NSArray *result, BOOL success))block {}
@end
Advertisements