Skip to content

Instantly share code, notes, and snippets.

@andrewsardone
Created July 10, 2012 18:08
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 andrewsardone/3085222 to your computer and use it in GitHub Desktop.
Save andrewsardone/3085222 to your computer and use it in GitHub Desktop.
A perfect example of listening to your tests, and appropriately abandoning ship (i.e., rethink that design)
#import <SenTestingKit/SenTestingKit.h>
#import "OCMock.h"
#import "SenTestCase+MethodSwizzling.h"
#import <objc/runtime.h>
#import "NLWelcomeViewController.h"
#import "NLWelcomeFlowGoogleViewController.h"
#pragma mark Setup
static NSString * const mobileToken = @"<some token>";
static NSString * const mobileUsername = @"asardone%40nutshell.com";
static NSString * const emailAddress = @"asardone@nutshell.com";
@implementation UIViewController (NLWelcomeFlowGoogleViewControllerTests)
- (NLWelcomeViewController *)nl_welcomeViewController
{
static char *nl_welcomeViewControllerForTestsKey = "nl_welcomeViewControllerForTests";
id result = objc_getAssociatedObject(self, nl_welcomeViewControllerForTestsKey);
if (!result) {
result = [OCMockObject niceMockForClass:[NLWelcomeViewController class]];
objc_setAssociatedObject(self, nl_welcomeViewControllerForTestsKey, result, OBJC_ASSOCIATION_RETAIN);
}
return result;
}
@end
@interface FakeHTTPCookieStorage : NSObject
+ (NSHTTPCookieStorage *)sharedHTTPCookieStorage;
@end
@implementation FakeHTTPCookieStorage
+ (NSHTTPCookieStorage *)sharedHTTPCookieStorage
{
NSHTTPCookie *usernameCookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
@"api.nutshell.com", NSHTTPCookieDomain,
@"/google", NSHTTPCookiePath,
@"mobile_username", NSHTTPCookieName,
mobileUsername, NSHTTPCookieValue,
nil]];
NSHTTPCookie *tokenCookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
@"api.nutshell.com", NSHTTPCookieDomain,
@"/google", NSHTTPCookiePath,
@"mobile_token", NSHTTPCookieName,
mobileToken, NSHTTPCookieValue,
nil]];
NSArray *fakeCookies = [NSArray arrayWithObjects:
usernameCookie,
tokenCookie,
nil];
id fakeHTTPCookieStorage = [OCMockObject niceMockForClass:[NSHTTPCookieStorage class]];
[[[fakeHTTPCookieStorage stub] andReturn:fakeCookies] cookies];
return fakeHTTPCookieStorage;
}
@end
#pragma mark - Tests
@interface NLWelcomeFlowGoogleViewControllerTests : SenTestCase
@end
@implementation NLWelcomeFlowGoogleViewControllerTests
- (void)testGiveWelcomeViewControllerCredentialsOnSuccessfulGoogleAuth
{
// neighborhood
NSURL *authURL = [NSURL URLWithString:@"https://auth.example.com"];
NSString *authEmail = emailAddress;
// system under test
id googleVC = [[NLWelcomeFlowGoogleViewController alloc] initWithAuthenticateURL:authURL
email:authEmail];
// expectations
id welcomeVC = [googleVC nl_welcomeViewController];
[[welcomeVC expect] googleAuthScreenCompletedWithUsername:emailAddress password:mobileToken];
[self swizzleClassMethod:@selector(sharedHTTPCookieStorage)
inClass:[NSHTTPCookieStorage class]
withClassMethod:@selector(sharedHTTPCookieStorage)
fromClass:[FakeHTTPCookieStorage class]
executeBlock:^
{
UIWebView *fakeSuccessfulWebView = [OCMockObject niceMockForClass:[UIWebView class]];
[googleVC webViewDidFinishLoad:fakeSuccessfulWebView];
// assert
[welcomeVC verify];
}
];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment