iOS 单元测试异步 Api, MVP + Kiwi + Mock

摘要

现在大部分的iOS应用都是先从网络获取数据,在做相应的处理,数据的获取一般是在异步线程里做,所以做单元测试比较麻烦。这里主要介绍Kiwi 的用法,Kiwi 是 unit test 框架,已经封装好了很多XTest 的Api ,使用起来非常方便。项目结构是用MVP 架构的,因为这种架构比较容易进行测试。项目的测试Demo 已发到Github,https://github.com/Alimjan2009/AAMVPUnitTest

1. 准备好项目

iOS 单元测试异步 Api, MVP + Kiwi + Mock_第1张图片

测试demo

我的测试demo 功能很简单,点击获取信息按钮以后,发送http 请求获取数据,再展示到label 里边
相应的代码如下:

  • PlaceInfoViewController.m
//
//  PlaceInfoViewController.m
//  AAMVPUnitTest
//
#import "PlaceInfoViewController.h"
#import "PlaceInfoPresenter.h"
@interface PlaceInfoViewController ()
@property (weak, nonatomic) IBOutlet UITextField *place;
@property (weak, nonatomic) IBOutlet UILabel *result;
@end
@implementation PlaceInfoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _presenter = [[PlaceInfoPresenter alloc]initWithView:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onGetPlaceInfoPressed:(id)sender {
    [_presenter loadDate:_place.text];
}
-(void)showResult:(NSString*)res{
    _result.text = res;
}
@end
  • PlaceInfoViewController.h
#import 
#import "PlaceInfoPresenter.h"
@interface PlaceInfoViewController : UIViewController
@property (nonatomic, strong) PlaceInfoPresenter *presenter;
@end
  • PlaceInfoPresenter.m
//
//  PlaceInfoPresenter.m
//  AAMVPUnitTest
//

#import "PlaceInfoPresenter.h"
#import "AFNetworking.h"
@implementation PlaceInfoPresenter
- (instancetype)initWithView:(id) view
{
    self = [super init];
    if (self) {
        self.view = view;
    }
    return self;
}
-(void)loadDate:(NSString*)placeName{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    manager.responseSerializer =[AFJSONResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json",@"text/plain", @"text/html",@"text/json",@"text/javascript", nil];
    //2.设置登录参数
    NSDictionary *dict = @{ @"a":placeName};
    
    //3.请求
    [manager GET:@"http://gc.ditu.aliyun.com/geocoding" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *parseError = nil;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&parseError];
        
        if(_view!=nil)
            [_view showResult:[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]];
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
@end

  • PlaceInfoPresenter.h
//
//  PlaceInfoPresenter.h
//  AAMVPUnitTest
//
//  Created by Alimjan on 16/5/4.
//  Copyright © 2016年 Alimjan. All rights reserved.
//

#import 

@protocol PlaceInfoViewImpl
-(void)showResult:(NSString*)res;
@end

@protocol PlaceInfoPresenterImpl
-(void)loadDate:(NSString*)placeName;
@end

@interface PlaceInfoPresenter : NSObject
@property (nonatomic, strong) id view;

- (instancetype)initWithView:(id) view;
@end

2. 集成kiwi

Kiwi 用cocopods 安装非常方便。以下是我的Podfile

target 'AAMVPUnitTestTests' , :exclusive => true do
  pod 'Kiwi'
end

3. 写测试模块

创建一个m 文件,并按照kiwi 格式写测试模块。我们的presenter 获取数据成功后,会调用showresult ,所以我们只要判断showresult 是否有被调用,就能判断接口是否成功
要是不知道mock 是什么,可以查看Kiwi 的手册。

  • PlaceInfoTestSpec.m
//
//  PlaceInfoTestSpec.m
//  AAMVPUnitTest
//
//  Created by Alimjan on 16/5/4.
//  Copyright 2016年 Alimjan. All rights reserved.
//

#import 
#import "PlaceInfoViewController.h"
#import "PlaceInfoPresenter.h"
SPEC_BEGIN(PlaceInfoTestSpec)

describe(@"PlaceInfoTest", ^{
    it(@"place info presenter test", ^{
        //
        // mock the view and stub the showResult method
       // 我们要测试api, 不关心view, 所以我们可以mock view
        id viewMock = [KWMock mockForProtocol:@protocol(PlaceInfoViewImpl)];
        [ [viewMock should] conformToProtocol:@protocol(PlaceInfoViewImpl)];
        
        [viewMock stub:@selector(showResult:) ];
        
        // init presenter
        // 初始化我们的presenter
        PlaceInfoPresenter *presenter = [[PlaceInfoPresenter alloc]initWithView:viewMock];
        
        // send asnc request
        // 测试我们的 业务
        [presenter loadDate:@"北京"];
        
        //wait until the show result called
      // 等待3秒,知道show result 方法被调用。我们的presenter 获取数据成功后,会调用showresult ,所以我们只要判断showresult 是否有被调用,就能判断接口是否成功
        [[viewMock shouldEventuallyBeforeTimingOutAfter(3.0)]receive:@selector(showResult:) withCount:1];
      });
  });
SPEC_END

你可能感兴趣的:(iOS 单元测试异步 Api, MVP + Kiwi + Mock)