Last active
August 1, 2017 10:30
-
-
Save shepting/4479767 to your computer and use it in GitHub Desktop.
Use this Gist to test AFNetworking based network calls. Use the respective client to test the succes: for failure: blocks. Stub and expect to your hearts content.
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
// | |
// AFTestSpec.m | |
@interface AlwaysSucceedClient : AFHTTPClient | |
@end | |
@implementation AlwaysSucceedClient | |
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure | |
{ | |
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters]; | |
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; | |
success(operation, operation.responseData); | |
} | |
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure | |
{ | |
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters]; | |
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; | |
success(operation, operation.responseData); | |
} | |
@end | |
@interface AlwaysFailClient : AFHTTPClient | |
@end | |
@implementation AlwaysFailClient | |
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure | |
{ | |
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters]; | |
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; | |
failure(operation, operation.error); | |
} | |
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure | |
{ | |
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters]; | |
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; | |
failure(operation, operation.error); | |
} | |
SPEC_BEGIN(NetworkInterfaceSpec) | |
describe(@"NetworkInterface", ^{ | |
__block NetworkInterface *network; | |
__block AlwaysSucceedClient *succeed; | |
__block AlwaysFailClient *fail; | |
beforeEach(^{ | |
network = [NetworkInterface sharedInstance]; | |
NSURL *base = [NSURL URLWithString:@"http://www.baseurl.com"]; | |
succeed = (AlwaysSucceedClient *)[AlwaysSucceedClient clientWithBaseURL:base]; | |
fail = (AlwaysFailClient *)[AlwaysFailClient clientWithBaseURL:base]; | |
network.currentClient = succeed; | |
}); | |
context(@"uploadData:", ^{ | |
beforeEach(^{ | |
[[ContactSync sharedInstance] stub:@selector(markContactsAsUploaded)]; | |
}); | |
it(@"should use the correct url", ^{ | |
[[succeed should] receive:@selector(postPath:parameters:success:failure:) withArguments:@"/api/v1/sync/contacts.json", [KWAny any], [KWAny any], [KWAny any]]; | |
[network uploadData:nil]; | |
}); | |
it(@"should use the correct data", ^{ | |
NSDictionary *fakeData = @{@"keyName": @"values"}; | |
[[succeed should] receive:@selector(postPath:parameters:success:failure:) withArguments:[KWAny any], fakeData, [KWAny any], [KWAny any]]; | |
[network uploadData:fakeData]; | |
}); | |
it(@"should update uploaded status on success", ^{ | |
[[[ContactSync sharedInstance] shouldEventually] receive:@selector(markContactsAsUploaded)]; | |
[network uploadData:nil]; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment