Last active
August 29, 2015 13:56
-
-
Save enigmaticape/9220716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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