Skip to content

Instantly share code, notes, and snippets.

@thiagolioy
Created June 11, 2015 13:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagolioy/93486715e6c19ac07e78 to your computer and use it in GitHub Desktop.
Save thiagolioy/93486715e6c19ac07e78 to your computer and use it in GitHub Desktop.
How to use Specta, Expecta and OCMock . With ViewControllers and Helper|Utility Classes
#import <UIKit/UIKit.h>
#import <Specta.h>
#import <Expecta.h>
#import <OCMock.h>
#import "SessionManager.h"
SpecBegin(SessionManagerSpec)
describe(@"SessionManager", ^{
__block SessionManager *_sessionManager;
beforeAll(^{
_sessionManager = [SessionManager sharedManager];
[_sessionManager clean];
});
it(@"should have a valid sharedInstance manager", ^{
expect(_sessionManager).toNot.beNil();
});
it(@"should show if is empty",^{
BOOL isEmpty = [_sessionManager isEmpty];
expect(isEmpty).to.beTruthy();
});
it(@"should persist agency and account in storage", ^{
[_sessionManager storeAgency:@"something"
account:@"somethingElse"];
expect([_sessionManager isEmpty]).to.beFalsy();
});
it(@"should retrieve agency and account from storage", ^{
NSString *agency = @"validAgency";
NSString *account = @"validAccount";
[_sessionManager storeAgency:agency
account:account];
expect(_sessionManager.storedAgency).to.equal(agency);
expect(_sessionManager.storedAccount).to.equal(account);
});
it(@"should clean agency and account from storage", ^{
[_sessionManager storeAgency:@"something"
account:@"somethingElse"];
[_sessionManager clean];
expect([_sessionManager isEmpty]).to.beTruthy();
});
});
SpecEnd
#import <UIKit/UIKit.h>
#import <Specta.h>
#import <Expecta.h>
#import <OCMock.h>
#import "ViewController.h"
#import "UserHelper.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 instantiateViewControllerWithIdentifier:@"ViewController"];
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