Skip to content

Instantly share code, notes, and snippets.

@ainame
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ainame/3ca51dde36ea0e9e997e to your computer and use it in GitHub Desktop.
Save ainame/3ca51dde36ea0e9e997e to your computer and use it in GitHub Desktop.
非同期+HTTPリクエストをスタブしたいときに使うと便利なテストクラス
#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
#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