更新YHAssemblyAssistant及其测试样例。Review by 阿瑟。
Showing
6 changed files
with
144 additions
and
16 deletions
@@ -16,6 +16,11 @@ | @@ -16,6 +16,11 @@ | ||
16 | #define kMaxLocalRecoderFileSize (1024 * 300) // 本地持久化文件大小(300KB) | 16 | #define kMaxLocalRecoderFileSize (1024 * 300) // 本地持久化文件大小(300KB) |
17 | #define kMinInterval 60 | 17 | #define kMinInterval 60 |
18 | 18 | ||
19 | +extern NSString * const JsonKeyDataTypeDevice; | ||
20 | +extern NSString * const JsonKeyDataTypeStatus; | ||
21 | +extern NSString * const JsonKeyDataTypeEvents; | ||
22 | +extern NSString * const JsonKeyDataTypeErrors; | ||
23 | + | ||
19 | /*! | 24 | /*! |
20 | 用于组装数据 | 25 | 用于组装数据 |
21 | */ | 26 | */ |
@@ -42,6 +47,6 @@ | @@ -42,6 +47,6 @@ | ||
42 | 47 | ||
43 | - (void)uploadDiskData; | 48 | - (void)uploadDiskData; |
44 | 49 | ||
45 | -- (void)uploadImmedilyWhithEvent:(YHAnalyItemData *)itemData; | 50 | +- (void)uploadImmedilyWithEvent:(YHAnalyItemData *)itemData; |
46 | 51 | ||
47 | @end | 52 | @end |
@@ -268,7 +268,7 @@ static dispatch_queue_t persisitingQueue; | @@ -268,7 +268,7 @@ static dispatch_queue_t persisitingQueue; | ||
268 | [collectData writeToFile:self.eventFileName atomically:YES]; | 268 | [collectData writeToFile:self.eventFileName atomically:YES]; |
269 | } | 269 | } |
270 | 270 | ||
271 | -- (void)uploadImmedilyWhithEvent:(YHAnalyItemData *)itemData | 271 | +- (void)uploadImmedilyWithEvent:(YHAnalyItemData *)itemData |
272 | { | 272 | { |
273 | if (itemData.dataType == YHItemDataTypeEvent) { | 273 | if (itemData.dataType == YHItemDataTypeEvent) { |
274 | YHEvent *event = (YHEvent *)itemData; | 274 | YHEvent *event = (YHEvent *)itemData; |
@@ -12,11 +12,6 @@ | @@ -12,11 +12,6 @@ | ||
12 | #define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey" | 12 | #define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey" |
13 | #define kKeyUserDefaultsCustomInterval @"customInterval" | 13 | #define kKeyUserDefaultsCustomInterval @"customInterval" |
14 | 14 | ||
15 | -extern NSString * const JsonKeyDataTypeDevice; | ||
16 | -extern NSString * const JsonKeyDataTypeStatus; | ||
17 | -extern NSString * const JsonKeyDataTypeEvents; | ||
18 | -extern NSString * const JsonKeyDataTypeErrors; | ||
19 | - | ||
20 | typedef enum _YohoMobStatLogStrategy { | 15 | typedef enum _YohoMobStatLogStrategy { |
21 | LogStrategyAppLaunch = 0, //每次程序启动 | 16 | LogStrategyAppLaunch = 0, //每次程序启动 |
22 | LogStrategyCustom = 1, //根据时间间隔接口发送 | 17 | LogStrategyCustom = 1, //根据时间间隔接口发送 |
@@ -185,8 +185,8 @@ static dispatch_queue_t persisitingQueue; | @@ -185,8 +185,8 @@ static dispatch_queue_t persisitingQueue; | ||
185 | [self uploadDiskData]; | 185 | [self uploadDiskData]; |
186 | } else if (self.logStrategy == LogStrategyCustom) { // 间隔发送 | 186 | } else if (self.logStrategy == LogStrategyCustom) { // 间隔发送 |
187 | 187 | ||
188 | - double tmp = [[[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval] doubleValue]; | ||
189 | - NSLog(@"tmp = %f",tmp); | 188 | +// double tmp = [[[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval] doubleValue]; |
189 | +// NSLog(@"tmp = %f",tmp); | ||
190 | 190 | ||
191 | // 获取上次启动时服务端给的时间间隔 | 191 | // 获取上次启动时服务端给的时间间隔 |
192 | if ([[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval]) { | 192 | if ([[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval]) { |
@@ -194,7 +194,8 @@ static dispatch_queue_t persisitingQueue; | @@ -194,7 +194,8 @@ static dispatch_queue_t persisitingQueue; | ||
194 | }else { | 194 | }else { |
195 | self.currentInterval = kMinInterval; | 195 | self.currentInterval = kMinInterval; |
196 | } | 196 | } |
197 | - | 197 | + NSLog(@"currentInterval = %0.1f", self.currentInterval); |
198 | + | ||
198 | [self tryUploadDiskData]; | 199 | [self tryUploadDiskData]; |
199 | 200 | ||
200 | self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentInterval target:self selector:@selector(tryUploadDiskData) userInfo:nil repeats:YES]; | 201 | self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentInterval target:self selector:@selector(tryUploadDiskData) userInfo:nil repeats:YES]; |
@@ -229,7 +230,7 @@ static dispatch_queue_t persisitingQueue; | @@ -229,7 +230,7 @@ static dispatch_queue_t persisitingQueue; | ||
229 | 230 | ||
230 | // 立即发送策略 | 231 | // 立即发送策略 |
231 | if (self.logStrategy == LogStrategyImmedi) { | 232 | if (self.logStrategy == LogStrategyImmedi) { |
232 | - [[YHAssemblyAssistant shareInstance] uploadImmedilyWhithEvent:event]; | 233 | + [[YHAssemblyAssistant shareInstance] uploadImmedilyWithEvent:event]; |
233 | } else { | 234 | } else { |
234 | [[YHAssemblyAssistant shareInstance] saveItemData:event]; | 235 | [[YHAssemblyAssistant shareInstance] saveItemData:event]; |
235 | } | 236 | } |
@@ -247,7 +248,7 @@ static dispatch_queue_t persisitingQueue; | @@ -247,7 +248,7 @@ static dispatch_queue_t persisitingQueue; | ||
247 | error.param = param; | 248 | error.param = param; |
248 | 249 | ||
249 | if (self.logStrategy == LogStrategyImmedi) { | 250 | if (self.logStrategy == LogStrategyImmedi) { |
250 | - [[YHAssemblyAssistant shareInstance]uploadImmedilyWhithEvent:error]; | 251 | + [[YHAssemblyAssistant shareInstance] uploadImmedilyWithEvent:error]; |
251 | } else { | 252 | } else { |
252 | [[YHAssemblyAssistant shareInstance] saveItemData:error]; | 253 | [[YHAssemblyAssistant shareInstance] saveItemData:error]; |
253 | } | 254 | } |
@@ -11,7 +11,10 @@ | @@ -11,7 +11,10 @@ | ||
11 | #import "OCMock.h" | 11 | #import "OCMock.h" |
12 | #import "YHAssemblyAssistant.h" | 12 | #import "YHAssemblyAssistant.h" |
13 | 13 | ||
14 | -@interface YHAssemblyAssistantTests : XCTestCase | 14 | +@interface YHAssemblyAssistantTests : XCTestCase { |
15 | +@private | ||
16 | + id mock; | ||
17 | +} | ||
15 | 18 | ||
16 | @end | 19 | @end |
17 | 20 | ||
@@ -20,16 +23,125 @@ | @@ -20,16 +23,125 @@ | ||
20 | - (void)setUp { | 23 | - (void)setUp { |
21 | [super setUp]; | 24 | [super setUp]; |
22 | // Put setup code here. This method is called before the invocation of each test method in the class. | 25 | // Put setup code here. This method is called before the invocation of each test method in the class. |
26 | + NSLog(@"%@ setUp", self.name); | ||
27 | + mock = [OCMockObject mockForClass:[YHAssemblyAssistant class]]; | ||
28 | + XCTAssertNotNil(mock, @"Cannot create YHAssemblyAssistant mock"); | ||
23 | } | 29 | } |
24 | 30 | ||
25 | - (void)tearDown { | 31 | - (void)tearDown { |
26 | // Put teardown code here. This method is called after the invocation of each test method in the class. | 32 | // Put teardown code here. This method is called after the invocation of each test method in the class. |
27 | [super tearDown]; | 33 | [super tearDown]; |
34 | + NSLog(@"%@ tearDown", self.name); | ||
35 | +} | ||
36 | + | ||
37 | +#pragma mark - self property | ||
38 | + | ||
39 | +- (void)testDeviceNotNil { | ||
40 | + NSLog(@"%@ start", self.name); | ||
41 | + | ||
42 | + YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init]; | ||
43 | + YHDevice *device = [assistant device]; | ||
44 | + XCTAssertNotNil(device, @"YHAssemblyAssistant device can't be nil."); | ||
45 | + | ||
46 | + NSLog(@"%@ end", self.name); | ||
47 | +} | ||
48 | + | ||
49 | +- (void)testCurrentStatusNotNil { | ||
50 | + NSLog(@"%@ start", self.name); | ||
51 | + | ||
52 | + YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init]; | ||
53 | + YHStatus *status = [assistant currentStatus]; | ||
54 | + XCTAssertNotNil(status, @"YHAssemblyAssistant status can't be nil."); | ||
55 | + | ||
56 | + NSLog(@"%@ end", self.name); | ||
57 | +} | ||
58 | + | ||
59 | +- (void)testImmediUploadItemDicNotNil { | ||
60 | + NSLog(@"%@ start", self.name); | ||
61 | + | ||
62 | + YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init]; | ||
63 | + NSMutableDictionary *itemDic = [assistant immediUploadItemDic]; | ||
64 | + XCTAssertNotNil(itemDic, @"YHAssemblyAssistant immediUploadItemDic can't be nil."); | ||
65 | + | ||
66 | + NSLog(@"%@ end", self.name); | ||
67 | +} | ||
68 | + | ||
69 | +#pragma mark - 方法存在性验证 | ||
70 | + | ||
71 | +- (void)testCanPersisitingMethod { | ||
72 | + NSLog(@"%@ start", self.name); | ||
73 | + | ||
74 | + [[[mock stub] andReturnValue:@YES] canPersisiting]; | ||
75 | + BOOL bPersisting = [mock canPersisiting]; | ||
76 | + XCTAssert(bPersisting, @"canPersisiting doesn't return YES."); | ||
77 | + NSLog(@"%@ end", self.name); | ||
28 | } | 78 | } |
29 | 79 | ||
30 | -- (void)testExample { | ||
31 | - // This is an example of a functional test case. | ||
32 | - XCTAssert(YES, @"Pass"); | 80 | +- (void)testSaveItemDataMethodWithConstraint { |
81 | + NSLog(@"%@ start", self.name); | ||
82 | + | ||
83 | + YHAnalyItemData *data = [[YHAnalyItemData alloc] init]; | ||
84 | + [[mock stub] saveItemData:data]; | ||
85 | + [mock saveItemData:data]; | ||
86 | + | ||
87 | + NSLog(@"%@ end", self.name); | ||
33 | } | 88 | } |
34 | 89 | ||
90 | +- (void)testGetAllEventCountMethod { | ||
91 | + NSLog(@"%@ start", self.name); | ||
92 | + | ||
93 | + [[[mock stub] andReturnValue:@10] getAllEventCount]; | ||
94 | + NSUInteger count = [mock getAllEventCount]; | ||
95 | + XCTAssertEqual(count, 10, @"YHAssemblyAssistant getAllEventCount doesn't return 10."); | ||
96 | + | ||
97 | + NSLog(@"%@ end", self.name); | ||
98 | +} | ||
99 | + | ||
100 | +- (void)testGetUploadDataMethod { | ||
101 | + NSLog(@"%@ start", self.name); | ||
102 | + | ||
103 | + NSDictionary *data = @{@"key": @"value"}; | ||
104 | + [[[mock stub] andReturn:data] getUploadData]; | ||
105 | + NSDictionary *returndata = [mock getUploadData]; | ||
106 | + XCTAssertEqualObjects(data, returndata, @"YHAssemblyAssistant getUploadData doesn't return a dictionary with key:value."); | ||
107 | + | ||
108 | + NSLog(@"%@ end", self.name); | ||
109 | +} | ||
110 | + | ||
111 | +- (void)testPrepareImmediUploadDicMethod { | ||
112 | + NSLog(@"%@ start", self.name); | ||
113 | + | ||
114 | + [[mock stub] prepareImmediUploadDic]; | ||
115 | + [mock prepareImmediUploadDic]; | ||
116 | + | ||
117 | + NSLog(@"%@ end", self.name); | ||
118 | +} | ||
119 | + | ||
120 | +- (void)testUpdateImmediUploadDicMethod { | ||
121 | + NSLog(@"%@ start", self.name); | ||
122 | + | ||
123 | + [[mock stub] updateImmediUploadDic]; | ||
124 | + [mock updateImmediUploadDic]; | ||
125 | + | ||
126 | + NSLog(@"%@ end", self.name); | ||
127 | +} | ||
128 | + | ||
129 | +- (void)testUploadDiskDataMethod { | ||
130 | + NSLog(@"%@ start", self.name); | ||
131 | + | ||
132 | + [[mock stub] uploadDiskData]; | ||
133 | + [mock uploadDiskData]; | ||
134 | + | ||
135 | + NSLog(@"%@ end", self.name); | ||
136 | +} | ||
137 | + | ||
138 | +- (void)testUploadImmedilyWithEventMethodWithCOnstraint { | ||
139 | + NSLog(@"%@ start", self.name); | ||
140 | + | ||
141 | + YHAnalyItemData *data = [[YHAnalyItemData alloc] init]; | ||
142 | + [[mock stub] uploadImmedilyWithEvent:data]; | ||
143 | + [mock uploadImmedilyWithEvent:data]; | ||
144 | + | ||
145 | + NSLog(@"%@ end", self.name); | ||
146 | +} | ||
35 | @end | 147 | @end |
@@ -80,4 +80,19 @@ | @@ -80,4 +80,19 @@ | ||
80 | 80 | ||
81 | NSLog(@"%@ end", self.name); | 81 | NSLog(@"%@ end", self.name); |
82 | } | 82 | } |
83 | + | ||
84 | +#pragma mark - 分支测试 | ||
85 | + | ||
86 | +- (void)testStartWithAppIdMethodWithLogStrategyAppLaunch { | ||
87 | + NSLog(@"%@ start", self.name); | ||
88 | + | ||
89 | + NSLog(@"%@ end", self.name); | ||
90 | +} | ||
91 | + | ||
92 | +- (void)testStartWithAppIdMethodWithLogStrategyCustom { | ||
93 | + NSLog(@"%@ start", self.name); | ||
94 | + | ||
95 | + NSLog(@"%@ end", self.name); | ||
96 | +} | ||
97 | + | ||
83 | @end | 98 | @end |
-
Please register or login to post a comment