-
Notifications
You must be signed in to change notification settings - Fork 20
/
PXRequestTests.m
104 lines (71 loc) · 4.02 KB
/
PXRequestTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// PXPXRequestTests.m
// PXAPI
//
// Created by Ash Furrow on 2013-01-03.
// Copyright (c) 2013 500px. All rights reserved.
//
#import "PXRequestTests.h"
#import "OCMock.h"
#import "PXRequest.h"
@interface PXRequest (UnitTestAdditions)
-(NSURLConnection *)urlConnectionForURLRequest:(NSURLRequest *)request;
-(id)initWithURLRequest:(NSURLRequest *)urlRequest completion:(PXRequestCompletionBlock)completion;
+(void)generateNoConsumerKeyError:(PXRequestCompletionBlock)completionBlock;
@end
@implementation PXRequestTests
-(void)testURLConnectionStart
{
NSURLRequest *dummyURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
PXRequest *requestUnderTest = [[PXRequest alloc] initWithURLRequest:dummyURLRequest completion:nil];
id mockConnection = [OCMockObject niceMockForClass:[NSURLConnection class]];
[[mockConnection expect] start];
id partialRequestMock = (PXRequest *)[OCMockObject partialMockForObject:requestUnderTest];
[[[partialRequestMock stub] andReturn:mockConnection] urlConnectionForURLRequest:OCMOCK_ANY];
[partialRequestMock start];
[mockConnection verify];
}
-(void)testCompletionBlockIsCalledOnConnectionSuccess
{
__block BOOL completionBlockInvoked = NO;
NSURLRequest *dummyURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
PXRequest *requestUnderTest = [[PXRequest alloc] initWithURLRequest:dummyURLRequest completion:^(NSDictionary *results, NSError *error) {
completionBlockInvoked = YES;
STAssertNil(error, @"Completion block should not have error on connection failure, but doesn't.");
STAssertNotNil(results, @"Completion block should not have results for successful connection.");
}];
id mockConnection = [OCMockObject niceMockForClass:[NSURLConnection class]];
[[mockConnection expect] start];
id partialRequestMock = (PXRequest *)[OCMockObject partialMockForObject:requestUnderTest];
[[[partialRequestMock stub] andReturn:mockConnection] urlConnectionForURLRequest:OCMOCK_ANY];
id mockResponse = [OCMockObject mockForClass:[NSHTTPURLResponse class]];
[[[mockResponse expect] andReturnValue:@(200)] statusCode];
NSData *jsonData = [@"{\"result\": true}" dataUsingEncoding:NSUTF8StringEncoding];
[partialRequestMock start];
[partialRequestMock connection:mockConnection didReceiveResponse:mockResponse];
[partialRequestMock connection:mockConnection didReceiveData:jsonData];
[partialRequestMock connectionDidFinishLoading:mockConnection];
[mockConnection verify];
STAssertTrue(completionBlockInvoked, @"Completion block was not invoked when connection failed.");
}
-(void)testCompletionBlockIsCalledOnConnectionFailure
{
__block BOOL completionBlockInvoked = NO;
NSURLRequest *dummyURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
PXRequest *requestUnderTest = [[PXRequest alloc] initWithURLRequest:dummyURLRequest completion:^(NSDictionary *results, NSError *error) {
completionBlockInvoked = YES;
STAssertNil(error, @"Completion block should note have error on connection success.");
STAssertNil(results, @"Completion block should not have results for failed connection.");
}];
id mockConnection = [OCMockObject niceMockForClass:[NSURLConnection class]];
[[mockConnection expect] start];
id partialRequestMock = (PXRequest *)[OCMockObject partialMockForObject:requestUnderTest];
[[[partialRequestMock stub] andReturn:mockConnection] urlConnectionForURLRequest:OCMOCK_ANY];
id mockResponse = [OCMockObject mockForClass:[NSHTTPURLResponse class]];
[[[mockResponse expect] andReturnValue:@(404)] statusCode];
[partialRequestMock start];
[partialRequestMock connectionDidFinishLoading:mockConnection];
[mockConnection verify];
STAssertTrue(completionBlockInvoked, @"Completion block was not invoked when connection failed.");
}
@end