Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing a UIViewController with OCHamcrest and OCMock
//
// LeadArticleViewControllerTest.m
//
#import <SenTestingKit/SenTestingKit.h>
#define HC_SHORTHAND
#import <OCHamcrestIOS/OCHamcrestIOS.h>
#import <OCMock/OCMock.h>
#import <CoreData/CoreData.h>
#import "AppController.h"
#import "LeadArticlesViewController.h"
#import "TeamStreamViewController.h"
#import "Article.h"
@interface LeadArticlesViewControllerTest : SenTestCase {}
@end
@implementation LeadArticlesViewControllerTest
AppController *app;
LeadArticlesViewController *controller;
- (void)setUp {
[super setUp];
app = [AppController sharedAppController];
controller = [[LeadArticlesViewController alloc] init];
[Article clearArticlesInContextAndSave:[app moc]];
[controller loadView];
}
- (void)tearDown {
[controller release];
app = nil;
[super tearDown];
}
- (void)testIBOutletsAreWiredUp {
assertThat(controller.leadArticlesScrollView, notNilValue());
assertThat(controller.leadArticlesPageControl, notNilValue());
}
#pragma mark -
#pragma mark viewDidLoadTests
- (void)testViewDidUnload {
[controller viewDidLoad];
[controller viewDidUnload];
assertThatBool(controller.showingArticles, equalToBool(NO));
assertThat(controller.leadArticlesScrollView, nilValue());
assertThat(controller.leadArticlesPageControl, nilValue());
}
- (void)testViewDidLoadShowsArticles {
[app checkForUpdates]; // Populate articles for controller to show
assertThatBool(controller.showingArticles, equalToBool(NO));
[controller viewDidLoad];
// Verify state
assertThatBool(controller.showingArticles, equalToBool(YES));
assertThatBool(controller.leadArticlesScrollView.hidden, equalToBool(NO));
assertThatBool(controller.leadArticlesPageControl.hidden, equalToBool(NO));
assertThat(controller.leadArticlesPageControl, notNilValue());
// Verify showing content
assertThatInt(controller.leadArticlesPageControl.numberOfPages, greaterThan(NUM_INT(1)));
UILabel *label = (UILabel *)[controller.view viewWithTag:kLeadArticleLabelTag];
assertThat(label.text, doesNotContainString(@"Loading"));
assertThatInt([label.text length], greaterThan(NUM_INT(1)));
}
- (void)testViewDidLoadShowsLoading {
[app setIsUpdatingInBackground:YES];
[controller viewDidLoad];
assertThatBool(controller.leadArticlesPageControl.hidden, equalToBool(YES));
UILabel *label = (UILabel *)[controller.view viewWithTag:kLeadArticleLabelTag];
assertThat(label.text, containsString(@"Loading"));
assertThatBool(controller.showingArticles, equalToBool(YES));
[app setIsUpdatingInBackground:NO];
}
- (void)testViewDidLoadHasNothingToShowSoParentControllerShouldHideIt {
[app setIsUpdatingInBackground:NO];
[controller viewDidLoad];
assertThatBool(controller.showingArticles, equalToBool(NO));
}
#pragma mark -
#pragma mark IBAction tests
- (void)testArticleButtonCallsDidSelectLeadArticle {
[app checkForUpdates]; // Populate articles for controller to show
// Use a mock parentController to intercept the call to presentModalViewController
id mockController = [OCMockObject mockForClass:[UIViewController class]];
[[mockController stub] presentModalViewController:[OCMArg checkWithBlock:^(id articleController) {
NSString *urlToLoad = ((TeamStreamViewController *)articleController).initialPath;
assertThat(urlToLoad, containsString(@"http://bleacherreport.com/articles"));
return YES;
}] animated:YES];
[controller setParentController:mockController];
// Get the UIButton for the 2nd article in the scroll view
[controller viewDidLoad];
UIButton *button = (UIButton *)[controller.view viewWithTag:1]; // second article
assertThat(button, notNilValue());
// Click the button and verify presentModalViewController was called
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
[mockController verify];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment