Last active
August 29, 2015 14:22
-
-
Save thiagolioy/7c3008a1a3f47244147d to your computer and use it in GitHub Desktop.
ViewController Tests Example with Specta Expecta OCMock
This file contains 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
typedef void (^CompletionBlock)(NSArray *results); | |
@interface ApiClient : NSObject | |
-(void)fetchCatalog:(CompletionBlock)block; | |
@end | |
@implementation ApiClient | |
-(void)fetchCatalog:(CompletionBlock)block{ | |
block(@[@1,@2,@3]); | |
} | |
@end |
This file contains 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
@interface UserHelper : NSObject | |
-(BOOL)validUser; | |
-(BOOL)shouldNotBeCalled; | |
@end | |
@implementation UserHelper | |
-(BOOL)validUser{ | |
return NO; | |
} | |
-(BOOL)shouldNotBeCalled{ | |
return NO; | |
} | |
@end |
This file contains 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
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> | |
@property(nonatomic,strong)IBOutlet UITextField *usernameTextfield; | |
@property(nonatomic,strong)IBOutlet UITextField *passwordTextfield; | |
@property(nonatomic,strong)IBOutlet UIButton *loginButton; | |
@property(nonatomic,strong)IBOutlet UITableView *tableView; | |
-(IBAction)clickOnLoginButton; | |
@end | |
@interface ViewController () | |
@property(nonatomic,strong) NSArray *dataArray; | |
@property(nonatomic,strong) UserHelper *userHelper; | |
@property(nonatomic,strong) ApiClient *service; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self setupService]; | |
[self fetchSomething]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
-(void)setupService{ | |
_service = [ApiClient new]; | |
} | |
-(void)fetchSomething{ | |
[_service fetchCatalog:^(NSArray *results) { | |
_dataArray = results; | |
[_tableView reloadData]; | |
}]; | |
} | |
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ | |
return _dataArray.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ | |
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath]; | |
return cell; | |
} | |
-(IBAction)clickOnLoginButton{ | |
[_userHelper validUser]; | |
[_userHelper shouldNotBeCalled]; | |
} | |
@end |
This file contains 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
// | |
// ViewControllerSpec.m | |
// ItauPfCS | |
// | |
// Created by Thiago Lioy on 5/27/15. | |
// Copyright (c) 2015 Thiago Lioy. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
#import "ViewController.h" | |
#import "UserHelper.h" | |
#import <Specta.h> | |
#define EXP_SHORTHAND | |
#import <Expecta.h> | |
#import <OCMock.h> | |
#import "ApiClient.h" | |
@interface ViewController (Private) | |
@property(nonatomic,strong) NSArray *dataArray; | |
@property(nonatomic,strong) UserHelper *userHelper; | |
@property(nonatomic,strong) ApiClient *service; | |
-(void)fetchSomething; | |
@end | |
SpecBegin(ViewControllerSpec) | |
describe(@"ViewController", ^{ | |
__block ViewController *vc; | |
beforeAll(^{ | |
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; | |
UIViewController *controller = [mainStoryboard instantiateInitialViewController]; | |
vc = (ViewController *)(controller); | |
[vc view]; | |
}); | |
describe(@"handle login flow", ^{ | |
it(@"should exists", ^{ | |
expect(vc).toNot.beNil(); | |
}); | |
it(@"should have an username field", ^{ | |
expect(vc.usernameTextfield).toNot.beNil(); | |
}); | |
it(@"should have a password field", ^{ | |
expect(vc.passwordTextfield).toNot.beNil(); | |
}); | |
it(@"should have a button", ^{ | |
expect(vc.loginButton).toNot.beNil(); | |
}); | |
it(@"should trigger verify user after call to login", ^{ | |
id userHelper = [OCMockObject niceMockForClass:UserHelper.class]; | |
[[userHelper expect] validUser]; | |
vc.userHelper = userHelper; | |
[vc clickOnLoginButton]; | |
[userHelper verify]; | |
}); | |
}); | |
describe(@"handle tableview flow", ^{ | |
it(@"should have a tableview", ^{ | |
expect(vc.tableView).toNot.beNil(); | |
}); | |
it(@"should conforme to Table view delegate protocol", ^{ | |
expect(vc).conformTo(@protocol(UITableViewDelegate)); | |
}); | |
it(@"should respond to Table view delegate didselect method", ^{ | |
expect(vc).respondTo(@selector(tableView:didSelectRowAtIndexPath:)); | |
}); | |
it(@"should conforme to Table view datasource protocol", ^{ | |
expect(vc).conformTo(@protocol(UITableViewDataSource)); | |
}); | |
it(@"should respond to Table view Datasource methods", ^{ | |
expect(vc).respondTo(@selector(tableView:numberOfRowsInSection:)); | |
expect(vc).respondTo(@selector(tableView:cellForRowAtIndexPath:)); | |
}); | |
it(@"should have 3 cells with filled dataarray", ^{ | |
vc.dataArray = @[@1,@2,@3]; | |
[vc.tableView reloadData]; | |
NSInteger rows = [vc tableView:vc.tableView numberOfRowsInSection:0]; | |
expect(rows).to.equal(3); | |
}); | |
it(@"should have 3 cells with filled dataarray", ^{ | |
id apiClientMock = [OCMockObject niceMockForClass:ApiClient.class]; | |
[[apiClientMock stub] fetchCatalog:[OCMArg checkWithBlock:^BOOL(id obj) { | |
vc.dataArray = @[@1,@2]; | |
[vc.tableView reloadData]; | |
return YES; | |
}]]; | |
vc.service = apiClientMock; | |
[vc fetchSomething]; | |
[apiClientMock verify]; | |
NSInteger rows = [vc tableView:vc.tableView numberOfRowsInSection:0]; | |
expect(rows).to.equal(2); | |
}); | |
}); | |
}); | |
SpecEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment