iOS-单元测试
iOS-单元测试
麦兜卖鱼丸 发表于11个月前
iOS-单元测试
  • 发表于 11个月前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

前言

单元测试对于任何的应用或者系统都是必要的,只是我们在开发的时候,大多数都忽略它的重要性与必要性;单元测试可以尽可能的保证项目的质量以及业务流程的完整性,减少bug的产生。

 

本例使用XCTest,xcode自带测试框架;

(一)测试异步(AFNetworking异步请求);

(1)AFNetworking异步请求后台api,返回数据;

#import <Foundation/Foundation.h>

@interface TestService : NSObject

+ (void)testPostWy:(void (^)(id responseObject))sucess failure:(void (^)(id error))failure;

@end


#import "TestService.h"
#import "AFApiDotNet.h"

@implementation TestService

+ (void)testPostWy:(void (^)(id))sucess failure:(void (^)(id))failure {
    
    AFHTTPSessionManager *manager = [AFApiDotNet httpClient];
    
    [manager GET:@"http://c.m.163.com/recommend/getSubDocPic?from=toutiao&prog=LMA1&open=&openpath=&fn=1&passport=&devId=5JQZG5PgwYinhJwpDtJKli2CqJrnjKAhdzTBA6zNHRZPOwRoIXUmsUYPMV3I%2B%2FIj&offset=0&size=10&version=17.2&spever=false&net=wifi&lat=BA%2FHUi%2FT6BcektiKQUOupg%3D%3D&lon=%2B6UXFqODkP%2FBHvMCgZrokw%3D%3D&ts=1479041109&sign=h4oxEfd8zdXy69d0MIhyMVWCwvM7uP3pCXYLEYM3yU548ErR02zJ6%2FKXOnxX046I&encryption=1&canal=appstore" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (sucess) {
            
            sucess(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failure) {
            
            failure(error);
        }
    }];
}
@end

 

(2)XCTestCase单元测试,以下testNetworkOpening方法,使用XCTestExpectation类,进行异步代码的测试;

#import <XCTest/XCTest.h>
#import "TestService.h"

@interface TestVCTests : XCTestCase

@end

@implementation TestVCTests

XCTestExpectation *networkSuccessExpectation;

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

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

- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
        
        for (int i = 0; i < 20; i++) {
            
            NSLog(@"test -- %d", i);
        }
        
    }];
}


- (void)testNetworkOpening {
    
    networkSuccessExpectation = [self expectationWithDescription:@"api open"];
    
    [TestService testPostWy:^(id responseObject) {
        
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        if ([result isKindOfClass:[NSDictionary class]]) {
            
            [networkSuccessExpectation fulfill];
        }
        
    } failure:^(id error) {
        
        if ([error isKindOfClass:[NSError class]]) {
            
            [networkSuccessExpectation fulfill];
        }
    }];
    
    [self waitForExpectationsWithTimeout:10.f handler:^(NSError * _Nullable error) {
        
        NSLog(@"time out");
    }];
}
@end

 

待续;

共有 人打赏支持
粉丝 13
博文 56
码字总数 69333
×
麦兜卖鱼丸
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: