摘要
現(xiàn)在大部分的iOS應(yīng)用都是先從網(wǎng)絡(luò)獲取數(shù)據(jù)揍愁,在做相應(yīng)的處理蝴悉,數(shù)據(jù)的獲取一般是在異步線程里做,所以做單元測(cè)試比較麻煩。這里主要介紹Kiwi 的用法偷崩,Kiwi 是 unit test 框架,已經(jīng)封裝好了很多XTest 的Api ,使用起來非常方便撞羽。項(xiàng)目結(jié)構(gòu)是用MVP 架構(gòu)的阐斜,因?yàn)檫@種架構(gòu)比較容易進(jìn)行測(cè)試。項(xiàng)目的測(cè)試Demo 已發(fā)到Github诀紊,https://github.com/Alimjan2009/AAMVPUnitTest
1. 準(zhǔn)備好項(xiàng)目
測(cè)試demo
我的測(cè)試demo 功能很簡(jiǎn)單谒出,點(diǎn)擊獲取信息按鈕以后,發(fā)送http 請(qǐng)求獲取數(shù)據(jù)邻奠,再展示到label 里邊
相應(yīng)的代碼如下:
- PlaceInfoViewController.m
//
// PlaceInfoViewController.m
// AAMVPUnitTest
//
#import "PlaceInfoViewController.h"
#import "PlaceInfoPresenter.h"
@interface PlaceInfoViewController ()<PlaceInfoViewImpl>
@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 <UIKit/UIKit.h>
#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<PlaceInfoViewImpl>) 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.設(shè)置登錄參數(shù)
NSDictionary *dict = @{ @"a":placeName};
//3.請(qǐng)求
[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 <Foundation/Foundation.h>
@protocol PlaceInfoViewImpl
-(void)showResult:(NSString*)res;
@end
@protocol PlaceInfoPresenterImpl
-(void)loadDate:(NSString*)placeName;
@end
@interface PlaceInfoPresenter : NSObject<PlaceInfoPresenterImpl>
@property (nonatomic, strong) id<PlaceInfoViewImpl> view;
- (instancetype)initWithView:(id<PlaceInfoViewImpl>) view;
@end
2. 集成kiwi
Kiwi 用cocopods 安裝非常方便笤喳。以下是我的Podfile
target 'AAMVPUnitTestTests' , :exclusive => true do
pod 'Kiwi'
end
3. 寫測(cè)試模塊
創(chuàng)建一個(gè)m 文件,并按照kiwi 格式寫測(cè)試模塊碌宴。我們的presenter 獲取數(shù)據(jù)成功后杀狡,會(huì)調(diào)用showresult ,所以我們只要判斷showresult 是否有被調(diào)用,就能判斷接口是否成功
要是不知道m(xù)ock 是什么贰镣,可以查看Kiwi 的手冊(cè)呜象。
- PlaceInfoTestSpec.m
//
// PlaceInfoTestSpec.m
// AAMVPUnitTest
//
// Created by Alimjan on 16/5/4.
// Copyright 2016年 Alimjan. All rights reserved.
//
#import <Kiwi/Kiwi.h>
#import "PlaceInfoViewController.h"
#import "PlaceInfoPresenter.h"
SPEC_BEGIN(PlaceInfoTestSpec)
describe(@"PlaceInfoTest", ^{
it(@"place info presenter test", ^{
//
// mock the view and stub the showResult method
// 我們要測(cè)試api, 不關(guān)心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
// 測(cè)試我們的 業(yè)務(wù)
[presenter loadDate:@"北京"];
//wait until the show result called
// 等待3秒,知道show result 方法被調(diào)用恭陡。我們的presenter 獲取數(shù)據(jù)成功后蹬音,會(huì)調(diào)用showresult ,所以我們只要判斷showresult 是否有被調(diào)用,就能判斷接口是否成功
[[viewMock shouldEventuallyBeforeTimingOutAfter(3.0)]receive:@selector(showResult:) withCount:1];
});
});
SPEC_END