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

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import "YHCrashReporter.h"
#import "YH_Analytics.h"

@interface YHCrashReporterTests : XCTestCase{
@private
    YHCrashReporter *crashReporter;
    id partialMock;
    id analyticsMock;
}

@end

@implementation YHCrashReporterTests

- (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);
    crashReporter = [YHCrashReporter new];
    partialMock = OCMPartialMock(crashReporter);
    XCTAssertNotNil(crashReporter, @"Cannot create YHCrashReporter instance");
    
    analyticsMock = OCMClassMock([YH_Analytics class]);
    OCMStub([analyticsMock sharedInstance]).andReturn(analyticsMock);
}

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

#pragma mark - 方法存在性验证

- (void)testCallstackAsArrayMethod {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([partialMock callstackAsArray]);
    
    [crashReporter callstackAsArray];
    
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleSignalMethod {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([partialMock handleSignal:[OCMArg isKindOfClass:[NSDictionary class]]]);
    
    [crashReporter handleSignal:@{@"key" : @"value"}];
    
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleNSExceptionMethod {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([partialMock handleNSException:[OCMArg isKindOfClass:[NSDictionary class]]]);
    
    [crashReporter handleNSException:@{@"key" : @"value"}];
    
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

#pragma mark - 行为测试
#pragma mark -
- (void)testCallstackAsArrayMethodReturnNotNil {
    NSLog(@"%@ start", self.name);
    
    NSLog(@"%@ end", self.name);
}

#pragma mark -
- (void)testHandleSignalMethodWithInputNil {
    NSLog(@"%@ start", self.name);
    
    [[analyticsMock reject] logError:[OCMArg any] parameters:[OCMArg any]];
    
    [crashReporter handleSignal:nil];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleSignalMethodWithInputInvalid {
    NSLog(@"%@ start", self.name);
    
    [[analyticsMock reject] logError:[OCMArg any] parameters:[OCMArg any]];
    
    [crashReporter handleSignal:nil];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleSignalMethodWithInputDictionary {
    NSLog(@"%@ start", self.name);
    
    NSDictionary *userInfo = [NSDictionary dictionary];
    [[analyticsMock expect] logError:@"CRASH" parameters:@{JsonKeyErrorST : userInfo}];
    
    [crashReporter handleSignal:userInfo];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

#pragma mark -
- (void)testHandleNSExceptionMethodWithInputNil {
    NSLog(@"%@ start", self.name);
    
    [[analyticsMock reject] logError:[OCMArg any] parameters:[OCMArg any]];
    
    [crashReporter handleNSException:nil];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleNSExceptionMethodWithInputInvalid {
    NSLog(@"%@ start", self.name);
    
    [[analyticsMock reject] logError:[OCMArg any] parameters:[OCMArg any]];
    
    [crashReporter handleNSException:nil];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

- (void)testHandleNSExceptionMethodWithInputDictionary {
    NSLog(@"%@ start", self.name);
    
    NSDictionary *userInfo = [NSDictionary dictionary];
    [[analyticsMock expect] logError:@"CRASH" parameters:@{JsonKeyErrorST : userInfo}];
    
    [crashReporter handleNSException:userInfo];
    
    [analyticsMock verify];
    
    NSLog(@"%@ end", self.name);
}

@end