YH_AnalyticsLogicTests.m 5.23 KB
//
//  YH_AnalyticsLogicTests.m
//  YH_Analytics
//
//  Created by Zhou Rongjun on 15/4/16.
//  Copyright (c) 2015年 YOHO. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "OCMock.h"
#import <CoreLocation/CoreLocation.h>
#import "YHCrashReporter.h"
#import "YHAssemblyAssistant.h"
#import "YH_Analytics.h"

@interface YH_AnalyticsLogicTests : XCTestCase {
@private
    id assemblyAssistantMock;
    id crashReporterMock;
    id networkAssistantMock;
    YH_Analytics *analytics;
}

@end

@implementation YH_AnalyticsLogicTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    
    assemblyAssistantMock = OCMClassMock([YHAssemblyAssistant class]);
    OCMStub([assemblyAssistantMock sharedInstance]).andReturn(assemblyAssistantMock);
    
    crashReporterMock = OCMClassMock([YHCrashReporter class]);
    OCMStub([crashReporterMock sharedInstance]).andReturn(crashReporterMock);
    
    networkAssistantMock = OCMClassMock([YHNetworkAssistant class]);
    OCMStub([networkAssistantMock sharedInstance]).andReturn(networkAssistantMock);
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

#pragma mark - 逻辑分支测试

- (void)testStartWithAppIdMethodPrepareImmediUploadDic {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    
    OCMExpect([assemblyAssistantMock prepareImmediUploadDic]);
    
    [analytics startWithAppId:@"appId"];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testStartWithAppIdMethodWithLogStrategyAppLaunch {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyAppLaunch;

    OCMExpect([assemblyAssistantMock uploadDiskData]);
    
    [analytics startWithAppId:@"appId"];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testStartWithAppIdMethodWithLogStrategyCustom {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyCustom;
    
    OCMExpect([assemblyAssistantMock uploadDiskData]);
    
    [analytics startWithAppId:@"appId"];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testStartWithAppIdMethodWithLogStrategyImmedi {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyImmedi;
    
    OCMExpect([assemblyAssistantMock uploadDiskData]);
    
    [analytics startWithAppId:@"appId"];
    
    XCTAssertThrows([assemblyAssistantMock verify], @"StartWithAppId should not uploadDiskData when strategy is immediately.") ;
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogEventMethodWithLogStrategyImmedi {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyImmedi;
    
    OCMExpect([assemblyAssistantMock uploadImmedilyWithEvent:[OCMArg any]]);
    
    [analytics logEvent:@"eventId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogEventMethodWithLogStrategyAppLaunch {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyAppLaunch;
    
    OCMExpect([assemblyAssistantMock saveItemData:[OCMArg any]]);
    
    [analytics logEvent:@"eventId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogEventMethodWithLogStrategyCustom {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyCustom;
    
    OCMExpect([assemblyAssistantMock saveItemData:[OCMArg any]]);
    
    [analytics logEvent:@"eventId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogErrorMethodWithLogStrategyImmedi {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyImmedi;
    
    OCMExpect([assemblyAssistantMock uploadImmedilyWithEvent:[OCMArg any]]);
    
    [analytics logError:@"errorId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogErrorMethodWithLogStrategyAppLaunch {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyAppLaunch;
    
    OCMExpect([assemblyAssistantMock saveItemData:[OCMArg any]]);
    
    [analytics logError:@"errorId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLogErrorMethodWithLogStrategyCustom {
    NSLog(@"%@ start", self.name);
    
    analytics = [[YH_Analytics alloc] init];
    analytics.logStrategy = LogStrategyCustom;
    
    OCMExpect([assemblyAssistantMock saveItemData:[OCMArg any]]);
    
    [analytics logError:@"errorId" parameters:nil];
    
    OCMVerifyAll(assemblyAssistantMock);
    
    NSLog(@"%@ end", self.name);
}

@end