-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChainableOperationBaseTests.m
More file actions
184 lines (136 loc) · 6.3 KB
/
ChainableOperationBaseTests.m
File metadata and controls
184 lines (136 loc) · 6.3 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// ChainableOperationBaseTests.m
// CompoundOperationsExampleDev
//
// Created by Novik Gleb on 10.05.16.
// Copyright © 2016 Novik Gleb. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
// Test class
#import "ChainableOperationBase.h"
// Constants
static CGFloat const COODefaultTestTimeout = 0.1f;
@interface ChainableOperationBaseTests : XCTestCase
@property (nonatomic, strong) ChainableOperationBase *chainableOperationBase;
@property (nonatomic, strong) id chainableOperationBaseMock;
@property (nonatomic, strong) id mockChainableOperationInput;
@property (nonatomic, strong) id mockChainableOperationOutput;
@property (nonatomic, copy) NSString *data;
@property (nonatomic, copy) void(^outputDataProcessedCompletionBlock)(NSInvocation *invocation);
@end
@implementation ChainableOperationBaseTests
- (void)setUp {
[super setUp];
self.chainableOperationBase = [ChainableOperationBase new];
self.chainableOperationBaseMock = OCMPartialMock(self.chainableOperationBase);
self.mockChainableOperationInput = OCMProtocolMock(@protocol(ChainableOperationInput));
self.mockChainableOperationOutput = OCMProtocolMock(@protocol(ChainableOperationOutput));
self.chainableOperationBase.input = self.mockChainableOperationInput;
self.chainableOperationBase.output = self.mockChainableOperationOutput;
self.data = @"data";
__weak __typeof__(self) weakSelf = self;
self.outputDataProcessedCompletionBlock = ^(NSInvocation *invocation) {
ChainableOperationBaseOutputDataBlock outputDataBlock;
[invocation getArgument:&outputDataBlock atIndex:3];
outputDataBlock(weakSelf.data, nil);
};
Class concreteDataClass = [self.data class];
OCMStub([self.chainableOperationBaseMock inputDataClass]).andReturn(concreteDataClass);
OCMStub([self.mockChainableOperationInput obtainInputDataWithTypeValidationBlock:OCMOCK_ANY]).andReturn(self.data);
OCMStub([self.chainableOperationBaseMock processInputData:OCMOCK_ANY
completionBlock:OCMOCK_ANY]).andDo(self.outputDataProcessedCompletionBlock);
}
- (void)tearDown {
self.outputDataProcessedCompletionBlock = nil;
self.data = nil;
[self.mockChainableOperationInput stopMocking];
self.mockChainableOperationInput = nil;
[self.chainableOperationBaseMock stopMocking];
self.chainableOperationBaseMock = nil;
self.chainableOperationBase = nil;
[super tearDown];
}
#pragma mark - Creation
- (void)testThatChainableOperationBaseIsASubclassOfAsyncOperation {
// given
// when
BOOL isKindOfClassAsyncOperation = [self.chainableOperationBase isKindOfClass:[AsyncOperation class]];
BOOL isMemberOfClassAsyncOperation = [self.chainableOperationBase isMemberOfClass:[AsyncOperation class]];
//then
XCTAssertTrue(isKindOfClassAsyncOperation && !isMemberOfClassAsyncOperation);
}
- (void)testThatChainableOperationBaseConformsToChainableOperationProtocol {
// given
// when
BOOL conformsToChainableOperationProtocol = [self.chainableOperationBase conformsToProtocol:@protocol(ChainableOperation)];
//then
XCTAssertTrue(conformsToChainableOperationProtocol);
}
- (void)testThatChainableOperationBaseConformsToNSCopyingProtocol {
// given
// when
BOOL conformsToChainableOperationProtocol = [self.chainableOperationBase conformsToProtocol:@protocol(NSCopying)];
//then
XCTAssertTrue(conformsToChainableOperationProtocol);
}
#pragma mark - Template method
- (void)testThatChainaleOperationBaseCallsInputDataClassMethodWhenStarted {
// given
XCTestExpectation *expectation = [self expectationWithDescription:@"Operation finished"];
[self.chainableOperationBase setCompletionBlock:^{
[expectation fulfill];
}];
// when
[(ChainableOperationBase *)self.chainableOperationBaseMock start];
// then
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
OCMVerify([self.chainableOperationBaseMock inputDataClass]);
}];
}
- (void)testThatChainaleOperationBaseCallsProcessDataMethodWhenStarted {
// given
XCTestExpectation *expectation = [self expectationWithDescription:@"Operation finished"];
[self.chainableOperationBase setCompletionBlock:^{
[expectation fulfill];
}];
// when
[(ChainableOperationBase *)self.chainableOperationBaseMock start];
// then
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
OCMVerify([self.chainableOperationBaseMock processInputData:OCMOCK_ANY
completionBlock:OCMOCK_ANY]);
}];
}
- (void)testThatChainaleOperationBaseCallsProcessDataMethodWithCorrectParametersWhenStarted {
// given
XCTestExpectation *expectation = [self expectationWithDescription:@"Operation finished"];
[self.chainableOperationBase setCompletionBlock:^{
[expectation fulfill];
}];
// when
[(ChainableOperationBase *)self.chainableOperationBaseMock start];
// then
__weak __typeof__(self) weakSelf = self;
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
OCMVerify([weakSelf.chainableOperationBaseMock processInputData:[OCMArg checkWithBlock:^BOOL(id obj) {
return [obj isEqual:weakSelf.data];
}]
completionBlock:OCMOCK_ANY]);
}];
}
- (void)testThatChainaleOperationBaseMakeCorrectOutputAfterDataProcessing {
// given
XCTestExpectation *expectation = [self expectationWithDescription:@"Operation finished"];
[self.chainableOperationBase setCompletionBlock:^{
[expectation fulfill];
}];
// when
[(ChainableOperationBase *)self.chainableOperationBaseMock start];
// then
__weak __typeof__(self) weakSelf = self;
[self waitForExpectationsWithTimeout:COODefaultTestTimeout handler:^(NSError * _Nullable error) {
OCMVerify([weakSelf.mockChainableOperationOutput didCompleteChainableOperationWithOutputData:weakSelf.data]);
}];
}
@end