Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enigmaticape/9220716 to your computer and use it in GitHub Desktop.
Save enigmaticape/9220716 to your computer and use it in GitHub Desktop.
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
typedef void(^MockBlock)(void);
@interface Action : NSObject
@property (nonatomic, strong) MockBlock action;
@end
@implementation Action
@end
@interface OCMockBlockTests : XCTestCase
@end
@implementation OCMockBlockTests {
MockBlock _the_block;
id _the_mock;
}
- (void)setUp
{
[super setUp];
_the_mock = [OCMockObject mockForClass:[Action class]];
}
- (void)tearDown { [super tearDown]; }
- (void) test_andReturn {
__block BOOL block_ran = NO;
_the_block = ^ { block_ran = YES; };
[[[_the_mock stub] andReturn:_the_block] action];
XCTAssertNoThrow( [_the_mock action](), @"Nope" );
// Throws this :
// /<unknown>: Expected invocation with object return type. Did you mean to use andReturnValue: instead?
XCTAssertTrue( block_ran, @"threw instead" );
}
- (void) test_andReturnValue {
__block BOOL block_ran = NO;
_the_block = ^ { block_ran = YES; };
[[[_the_mock stub] andReturnValue:_the_block] action];
XCTAssertNoThrow( [_the_mock action](), @"Also nope" );
// Throws this :
// /<unknown>: -[__NSMallocBlock__ objCType]: unrecognized selector sent to instance 0x8bafe10
XCTAssertTrue( block_ran, @"threw instead" );
}
-(void) test_setReturnValue {
__block BOOL block_ran = NO;
_the_block = ^ { block_ran = YES; };
void (^invocation_block)(NSInvocation *) = ^(NSInvocation *invocation) {
[invocation setReturnValue:&_the_block];
};
[[[_the_mock stub] andDo:invocation_block] action];
[_the_mock action]();
XCTAssertTrue( block_ran, @"Yup");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment