Authored by 周蓉君

更新YHAssemblyAssistant及其测试样例。Review by 阿瑟。

... ... @@ -16,6 +16,11 @@
#define kMaxLocalRecoderFileSize (1024 * 300) // 本地持久化文件大小(300KB)
#define kMinInterval 60
extern NSString * const JsonKeyDataTypeDevice;
extern NSString * const JsonKeyDataTypeStatus;
extern NSString * const JsonKeyDataTypeEvents;
extern NSString * const JsonKeyDataTypeErrors;
/*!
用于组装数据
*/
... ... @@ -42,6 +47,6 @@
- (void)uploadDiskData;
- (void)uploadImmedilyWhithEvent:(YHAnalyItemData *)itemData;
- (void)uploadImmedilyWithEvent:(YHAnalyItemData *)itemData;
@end
... ...
... ... @@ -268,7 +268,7 @@ static dispatch_queue_t persisitingQueue;
[collectData writeToFile:self.eventFileName atomically:YES];
}
- (void)uploadImmedilyWhithEvent:(YHAnalyItemData *)itemData
- (void)uploadImmedilyWithEvent:(YHAnalyItemData *)itemData
{
if (itemData.dataType == YHItemDataTypeEvent) {
YHEvent *event = (YHEvent *)itemData;
... ...
... ... @@ -12,11 +12,6 @@
#define kKeyUserDefaultsLastUploadTimestamp @"lastSendTimestampKey"
#define kKeyUserDefaultsCustomInterval @"customInterval"
extern NSString * const JsonKeyDataTypeDevice;
extern NSString * const JsonKeyDataTypeStatus;
extern NSString * const JsonKeyDataTypeEvents;
extern NSString * const JsonKeyDataTypeErrors;
typedef enum _YohoMobStatLogStrategy {
LogStrategyAppLaunch = 0, //每次程序启动
LogStrategyCustom = 1, //根据时间间隔接口发送
... ...
... ... @@ -185,8 +185,8 @@ static dispatch_queue_t persisitingQueue;
[self uploadDiskData];
} else if (self.logStrategy == LogStrategyCustom) { // 间隔发送
double tmp = [[[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval] doubleValue];
NSLog(@"tmp = %f",tmp);
// double tmp = [[[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval] doubleValue];
// NSLog(@"tmp = %f",tmp);
// 获取上次启动时服务端给的时间间隔
if ([[NSUserDefaults standardUserDefaults] objectForKey:kKeyUserDefaultsCustomInterval]) {
... ... @@ -194,6 +194,7 @@ static dispatch_queue_t persisitingQueue;
}else {
self.currentInterval = kMinInterval;
}
NSLog(@"currentInterval = %0.1f", self.currentInterval);
[self tryUploadDiskData];
... ... @@ -229,7 +230,7 @@ static dispatch_queue_t persisitingQueue;
// 立即发送策略
if (self.logStrategy == LogStrategyImmedi) {
[[YHAssemblyAssistant shareInstance] uploadImmedilyWhithEvent:event];
[[YHAssemblyAssistant shareInstance] uploadImmedilyWithEvent:event];
} else {
[[YHAssemblyAssistant shareInstance] saveItemData:event];
}
... ... @@ -247,7 +248,7 @@ static dispatch_queue_t persisitingQueue;
error.param = param;
if (self.logStrategy == LogStrategyImmedi) {
[[YHAssemblyAssistant shareInstance]uploadImmedilyWhithEvent:error];
[[YHAssemblyAssistant shareInstance] uploadImmedilyWithEvent:error];
} else {
[[YHAssemblyAssistant shareInstance] saveItemData:error];
}
... ...
... ... @@ -11,7 +11,10 @@
#import "OCMock.h"
#import "YHAssemblyAssistant.h"
@interface YHAssemblyAssistantTests : XCTestCase
@interface YHAssemblyAssistantTests : XCTestCase {
@private
id mock;
}
@end
... ... @@ -20,16 +23,125 @@
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
NSLog(@"%@ setUp", self.name);
mock = [OCMockObject mockForClass:[YHAssemblyAssistant class]];
XCTAssertNotNil(mock, @"Cannot create YHAssemblyAssistant mock");
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
NSLog(@"%@ tearDown", self.name);
}
#pragma mark - self property
- (void)testDeviceNotNil {
NSLog(@"%@ start", self.name);
YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init];
YHDevice *device = [assistant device];
XCTAssertNotNil(device, @"YHAssemblyAssistant device can't be nil.");
NSLog(@"%@ end", self.name);
}
- (void)testCurrentStatusNotNil {
NSLog(@"%@ start", self.name);
YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init];
YHStatus *status = [assistant currentStatus];
XCTAssertNotNil(status, @"YHAssemblyAssistant status can't be nil.");
NSLog(@"%@ end", self.name);
}
- (void)testImmediUploadItemDicNotNil {
NSLog(@"%@ start", self.name);
YHAssemblyAssistant *assistant = [[YHAssemblyAssistant alloc] init];
NSMutableDictionary *itemDic = [assistant immediUploadItemDic];
XCTAssertNotNil(itemDic, @"YHAssemblyAssistant immediUploadItemDic can't be nil.");
NSLog(@"%@ end", self.name);
}
- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
#pragma mark - 方法存在性验证
- (void)testCanPersisitingMethod {
NSLog(@"%@ start", self.name);
[[[mock stub] andReturnValue:@YES] canPersisiting];
BOOL bPersisting = [mock canPersisiting];
XCTAssert(bPersisting, @"canPersisiting doesn't return YES.");
NSLog(@"%@ end", self.name);
}
- (void)testSaveItemDataMethodWithConstraint {
NSLog(@"%@ start", self.name);
YHAnalyItemData *data = [[YHAnalyItemData alloc] init];
[[mock stub] saveItemData:data];
[mock saveItemData:data];
NSLog(@"%@ end", self.name);
}
- (void)testGetAllEventCountMethod {
NSLog(@"%@ start", self.name);
[[[mock stub] andReturnValue:@10] getAllEventCount];
NSUInteger count = [mock getAllEventCount];
XCTAssertEqual(count, 10, @"YHAssemblyAssistant getAllEventCount doesn't return 10.");
NSLog(@"%@ end", self.name);
}
- (void)testGetUploadDataMethod {
NSLog(@"%@ start", self.name);
NSDictionary *data = @{@"key": @"value"};
[[[mock stub] andReturn:data] getUploadData];
NSDictionary *returndata = [mock getUploadData];
XCTAssertEqualObjects(data, returndata, @"YHAssemblyAssistant getUploadData doesn't return a dictionary with key:value.");
NSLog(@"%@ end", self.name);
}
- (void)testPrepareImmediUploadDicMethod {
NSLog(@"%@ start", self.name);
[[mock stub] prepareImmediUploadDic];
[mock prepareImmediUploadDic];
NSLog(@"%@ end", self.name);
}
- (void)testUpdateImmediUploadDicMethod {
NSLog(@"%@ start", self.name);
[[mock stub] updateImmediUploadDic];
[mock updateImmediUploadDic];
NSLog(@"%@ end", self.name);
}
- (void)testUploadDiskDataMethod {
NSLog(@"%@ start", self.name);
[[mock stub] uploadDiskData];
[mock uploadDiskData];
NSLog(@"%@ end", self.name);
}
- (void)testUploadImmedilyWithEventMethodWithCOnstraint {
NSLog(@"%@ start", self.name);
YHAnalyItemData *data = [[YHAnalyItemData alloc] init];
[[mock stub] uploadImmedilyWithEvent:data];
[mock uploadImmedilyWithEvent:data];
NSLog(@"%@ end", self.name);
}
@end
... ...
... ... @@ -80,4 +80,19 @@
NSLog(@"%@ end", self.name);
}
#pragma mark - 分支测试
- (void)testStartWithAppIdMethodWithLogStrategyAppLaunch {
NSLog(@"%@ start", self.name);
NSLog(@"%@ end", self.name);
}
- (void)testStartWithAppIdMethodWithLogStrategyCustom {
NSLog(@"%@ start", self.name);
NSLog(@"%@ end", self.name);
}
@end
... ...