Last active
August 29, 2015 14:01
-
-
Save ainame/3ca51dde36ea0e9e997e to your computer and use it in GitHub Desktop.
非同期+HTTPリクエストをスタブしたいときに使うと便利なテストクラス
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
#import <XCTest/XCTest.h> | |
#import <TKRGuard.h> | |
#import <OHHTTPStubs.h> | |
@interface AMEHTTPRequestTestCase : XCTestCase | |
@property (nonatomic, strong) NSURLRequest *lastRequest; | |
- (void)stubAllRequestWithResponseFileName:(NSString *)responseFileName statuCode:(NSInteger)statusCode; | |
@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
#import "AMEHTTPRequestTestCase.h" | |
#import <OHHTTPStubs/OHHTTPStubsResponse.h> | |
@implementation AMEHTTPRequestTestCase | |
- (void)setUp | |
{ | |
[super setUp]; | |
// Put setup code here. This method is called before the invocation of each test method in the class. | |
} | |
- (void)tearDown | |
{ | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
[super tearDown]; | |
self.lastRequest = nil; | |
[OHHTTPStubs removeAllStubs]; | |
} | |
- (void)stubAllRequestWithResponseFileName:(NSString *)responseFileName statuCode:(NSInteger)statusCode | |
{ | |
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return YES; } | |
withStubResponse:^OHHTTPStubsResponse * (NSURLRequest *request) { | |
self.lastRequest = request; | |
return [OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(responseFileName, nil) | |
statusCode:(int)statusCode | |
headers:@{@"Content-Type" : @"application/json"}]; | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment