Skip to content

Instantly share code, notes, and snippets.

@nwg
Created March 7, 2012 23:38
Show Gist options
  • Select an option

  • Save nwg/1997301 to your computer and use it in GitHub Desktop.

Select an option

Save nwg/1997301 to your computer and use it in GitHub Desktop.
@implementation ClassMockTest
@synthesize mockAlert = mockAlert_;
- (BOOL)shouldRunOnMainThread { return YES; }
- (void)showAlert {
UIAlertView *alertView = [UIAlertView alloc];
alertView = [alertView initWithTitle:nil message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
}
- (void)setUp {
[UIAlertView startMockingClass];
self.mockAlert = [OCMockObject mockForClass:[UIAlertView class]];
}
- (void)tearDown {
[UIAlertView verify];
[UIAlertView stopMockingClass];
[self.mockAlert verify];
}
- (void)handleException:(NSException *)exception {
[ClassMockForwarder removeAllMocks];
}
- (void)testSomething {
// Expect an allocation
[[[UIAlertView expect] andReturnFromBlock:^{ return self.mockAlert; }] alloc];
[[[self.mockAlert expect] andReturn:self.mockAlert] initWithTitle:OCMOCK_ANY message:OCMOCK_ANY delegate:OCMOCK_ANY cancelButtonTitle:OCMOCK_ANY otherButtonTitles:OCMOCK_ANY, nil];
// Alert should be shown
[[self.mockAlert expect] show];
[self showAlert];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment