-
-
Save gauravds/6bf0e61cc52a31c43ab9b8a930571393 to your computer and use it in GitHub Desktop.
Objective-C iOS API Mocking using XCTest (Xcode 8.1) & OHHTTPStub v5.2.3
This file contains hidden or 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
{ | |
"address": "4/16-D, Civil Lines, Airport Road", | |
"city": "Jaipur", | |
"created_at": "2016-03-15T09:58:26Z", | |
"email": "[email protected]", | |
"email_verified": false, | |
"first_name": "Test", | |
"gender": "male", | |
"id": 123456, | |
"last_name": "User", | |
"state": "Rajasthan", | |
"updated_at": "2016-03-15T09:58:26Z", | |
"zip_code": "302001", | |
"authentication_token": "test_auth_token", | |
"referral_code": "test_referral_code", | |
} |
This file contains hidden or 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
// | |
// MockAPITest.m | |
// iOS-API-Mock-Test | |
// | |
// Created by gauravds on 28/12/16. | |
// Copyright © 2016 iOS Dev Group. All rights reserved. | |
// | |
#import "ModelTestHelper.h" | |
#define kLoginData @"LoginData" | |
#define kLoginDataStaging @"LoginDataStaging" | |
@interface MockAPITest : ModelTestHelper | |
@end | |
@implementation MockAPITest | |
- (void)testLogin { | |
XCTestExpectation *expectation = [self stubFileName:@"loginData.json"]; | |
//-- Here API is called and response will come through Stub. | |
[[UserManager sharedInstance] loginWithEmail:@"[email protected]" | |
password:@"password123" | |
response:^(BOOL result, id result) { | |
[expectation fulfill]; | |
}]; | |
[self waitForExpectationsWithTimeout:self.timeout | |
handler:^(NSError *error) { | |
if (error) { | |
NSLog(@"XCTest Request Timeout Error: %@", error); | |
} | |
}]; | |
} | |
- (void)testReadFile { | |
// -- prints string content of file in test project | |
NSLog(@"loginData read from file : %@", [self readFile:@"loginData.json"]); | |
} | |
@end |
This file contains hidden or 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
// | |
// ModelTestHelper.h | |
// iOS-API-Mock-Test | |
// | |
// Created by gauravds on 28/12/16. | |
// Copyright © 2016 iOS Dev Group. All rights reserved. | |
// | |
#import <XCTest/XCTest.h> | |
@interface ModelTestHelper : XCTestCase | |
- (double)timeout; | |
- (NSString*)readFile:(NSString*)filename; | |
- (XCTestExpectation*)stubFileName:(NSString*)fileName; | |
- (XCTestExpectation*)stubCode:(int)statusCode fileName:(NSString*)fileName; | |
- (XCTestExpectation*)stubCode:(int)statusCode fileName:(NSString*)fileName header:(NSDictionary*)headers; | |
@end |
This file contains hidden or 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
// | |
// ModelTestHelper.m | |
// iOS-API-Mock-Test | |
// | |
// Created by gauravds on 28/12/16. | |
// Copyright © 2016 iOS Dev Group. All rights reserved. | |
// | |
#import "ModelTestHelper.h" | |
#import <OHHTTPStubs/OHHTTPStubs.h> | |
#import <OHHTTPStubs/OHPathHelpers.h> | |
@implementation ModelTestHelper | |
- (void)tearDown { | |
[OHHTTPStubs removeAllStubs]; | |
[super tearDown]; | |
} | |
#pragma mark - Common | |
- (double)timeout { | |
return 10.0; | |
} | |
- (NSString*)readFile:(NSString*)filename { | |
NSBundle *bundle = [NSBundle bundleForClass:self.class]; | |
NSString *path = [bundle pathForResource:filename ofType:nil]; | |
return [NSString stringWithContentsOfFile:path | |
encoding:NSUTF8StringEncoding | |
error:NULL]; | |
} | |
#pragma mark - OHHTTPStub | |
- (XCTestExpectation*)stubFileName:(NSString*)fileName { | |
return [self stubCode:200 fileName:fileName header:nil]; | |
} | |
- (XCTestExpectation*)stubCode:(int)statusCode fileName:(NSString*)fileName { | |
return [self stubCode:statusCode fileName:fileName header:nil]; | |
} | |
- (XCTestExpectation*)stubCode:(int)statusCode fileName:(NSString*)fileName header:(NSDictionary*)headers { | |
NSMutableDictionary *headersMut = [@{@"Content-Type": @"application/json"} mutableCopy]; | |
if (headers) { | |
[headersMut addEntriesFromDictionary:headers]; | |
} | |
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { | |
return YES; | |
} | |
withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) { | |
return [[OHHTTPStubsResponse alloc] initWithFileAtPath:OHPathForFile(fileName, self.class) | |
statusCode:statusCode | |
headers:headersMut]; | |
}]; | |
return [self expectationWithDescription:@"API response expectation"]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment