+1

Ứng dụng BDD trong lập trình iOS

Testing đóng 1 vai trò quan trọng trong quá trình phát triển phần mềm. Hôm nay tôi sẽ giới thiệu với các bạn ứng dụng BDD test trong lập trình iOS.

Giả sử ta muốn test 1 phép cộng đơn giản:

File H

// // ViewController.h

// HelloKiwi

//

// Created by nguyen hai dang on 1/23/15.

// Copyright (c) 2015 noveo. All rights reserved.

//

import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) AFHTTPRequestOperationManager *manager;

  • (NSUInteger)sumOf:(NSUInteger)firstA and:(NSUInteger)secondA;

@end File m

// // ViewController.m // HelloKiwi // // Created by nguyen hai dang on 1/23/15. // Copyright (c) 2015 noveo. All rights reserved. //

import "ViewController.h"

import "RepoVC.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad]; }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

-(NSUInteger)sumOf:(NSUInteger)firstA and:(NSUInteger)secondB { return (firstA + secondB); } Sử dụng UnitTest của apple ta được:

//

// TestTDD.m

// HelloKiwi

//

// Created by nguyen hai dang on 1/23/15.

// Copyright (c) 2015 noveo. All rights reserved.

//

import <UIKit/UIKit.h>

import <XCTest/XCTest.h>

import "ViewController.h"

@interface TestTDD : XCTestCase

@property (nonatomic, strong)ViewController *vc;

@end

@implementation TestTDD

  • (void)setUp { [super setUp]; _vc = [ViewController new]; }

  • (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. XCTAssertEqual(43, [_vc sumOf:16 and:42]); }

  • (void)testPerformanceExample { // This is an example of a performance test case. [self measureBlock:^{ // Put the code you want to measure the time of here. }]; }

@end Như bạn thấy nếu sử dụng unittest thông thường của Apple để test các method thì việc viết test case đôi khi gây khó hiểu, dài , và không rõ được flow trạng thái của code. Nếu bạn muốn viết chi tiết hơn thì phải thêm comment trong code.

BDD Test - Kiwi

Các bạn download kiwi tại https://github.com/kiwi-bdd/Kiwi/wiki/Getting-Started-with-Kiwi-2.0 và cài đặt như trên trang chủ

Sau đây là đoạn code sử dụng kiwi để viết testcase method trên:

//

// TestWiki.m

// HelloKiwi

//

// Created by nguyen hai dang on 1/23/15.

// Copyright (c) 2015 noveo. All rights reserved.

//

import "Kiwi.h"

import "ViewController.h"

SPEC_BEGIN(TestSum);

describe(@"TestSum", ^{

ViewController *vc = [ViewController new];

it(@"Test Sum", ^{

    NSUInteger a = 16;
    NSUInteger b = 26;

    [[theValue([vc sumOf:a and:b])should]equal:theValue(43)];

});

});

SPEC_END Như các bạn thấy kiwi hỗ trợ viết test case một cách tường minh, dễ hiểu giúp bạn dễ viết test case hơn. Ngoài ra nó còn có nhiều lợi ích sau:

  • Không giới hạn số lượng setup và teardown: beforeEach(^{ /* ... / }); beforeAll(^{ / ... / }); afterEach(^{ / ... / }); afterAll(^{ / ... */ });
  • Mocks and stubs included
  • Asynchronous testing support
  • Dễ đọc hơn XCTest Với kiwi tôi nghĩ chúng ta có thể tiết kiệm không gian và thời gian viết test case trong quá trình phát triển. Cám ơn đã theo dõi bài viết này 😃

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí