Quick and Nimble Brief Explanation in Swift

# Using Quick and Nimble for Unit Testing in iOS project
As I was discussing in my previous article (<a href="https://qiita.com/phanithken/items/af097a17768f8f500888">IOS Test Frameworks and Unit Testing Brief Explanation</a>) about the built-in XCTest framework, there are a lot more easier using XCTest but as a developer we always try to find ways that is easier and clean for us to write the code. That's why I am now introducing <b>Quick and Nimble for Unit Testing</b>. Quick and Nimble provide similar capability of doing the Unit Testing in iOS when compare to XCTest, but you will find it more useful as you go and play with it. I've been playing around with it for a while and I feel like I'm not gonna use the XCTest framework again :) Sound bad right!!!
## Comparison between XCTest vs Quick and Nimble
<b>XCTest</b> is a testing framework provided by Apple. It is also pretty straightforward but you need to be aware of setup() and tearDown() functions. XCTAssertions are also pretty inconvenient to use. If you take a look at the naming of the test methods, it is also kind of complicated to descriptively capture the test case not having very long function names.

<b>Quick</b> is a testing framework which provides a convenient method to compose a unit test. It uses the single function spec() to define the whole test. The spec() function is composed of multiple sections. The sections are describe, context and it. I will explain the purpose and benefits of such a setup later in this article.

<b> Nimble </b> is a matching framework which provides plenty of options how to fulfill your test expectations. The keyword expect is very important, because it replaces XCTAssertion of XCTest.

## How to use Quick and Nimble
First, you have to install Quick and Nimble. You can either use <b>Cathage</b> or <b>Cocoa Pod</b>, but as I am more familiar with <b>Cocoa Pod</b>, I will use it in this article.

```swift
pod "Quick"
pod "Nimble"
```
Now go ahead an navigate to the <b>Build Phase</b> and add Link Library as shown in the picture below: ![1*4aH-beuiPnYsqfEQ3lvynA.png](https://qiita-image-store.s3.amazonaws.com/0/211892/68b41d94-926d-140a-bb6d-11c53f2c2329.png)
Now you are ready to create your own Unit Test. I'll be following with the same Sample Project <a href="https://github.com/phanithken/ArithmeticUnitTest">ArithmeticUnitTest</a> I used in my previous article.
So now let's go and create a new file in the Unit Test directory and name it as <b>ArithmeticSpec.swift</b>

```swift
import Foundation
import Quick
import Nimble

@testable import SimpleUnitTest

class ArithmeticSpec: QuickSpec {
    
    override func spec() {
        // we will perform the unit testing here
    }
    
}
```
As you can see in the above code, It's really similar to XCTest, but we only have to override <b>spec()</b> in order to go. Cool !!!!
Before adding the code, I would like to explain some of the important keywords as we will use in this test.
<b>describe</b> : exactly describes what component you are testing.
<b>context</b> : describes the purpose of the test or the current state of an object.
<b>it</b> : describes the expected result of the test.
And <b>expect</b> keyword is very important. It's descriptively express the expected result we want to happen.

```swift
import Foundation
import Quick
import Nimble

@testable import SimpleUnitTest

class ArithmeticSpec: QuickSpec {
    
    override func spec() {
        describe("Arithmetic Progression") {
            context("addition process should be performed") {
                let arithmetic: Arithmetic = Arithmetic(a: 10, b: 5)
                it("should add two int together") {
                    expect(arithmetic.addition()).to(equal(15))
                }
            }
            
            context("division process should be performed") {
                let arithmetic: Arithmetic = Arithmetic(a: 10, b: 5)
                it("should divide two int") {
                    expect(arithmetic.division()).to(equal(2))
                }
            }
            
            context("multiplication process should be performed") {
                let arithmetic: Arithmetic = Arithmetic(a: 10, b: 5)
                it("should multiply two int together") {
                    expect(arithmetic.multiplication()).to(equal(50))
                }
            }
            
            context("substraction process should be performed") {
                let arithmetic: Arithmetic = Arithmetic(a: 10, b: 5)
                it("should substract two int") {
                    expect(arithmetic.substraction()).to(equal(5))
                }
            }
        }
        
        describe("TableViewControllerSpec") {
            context("table view should display some row") {
                let viewController: ViewController = ViewController()
                it("should show three rows in the table view") {
                    expect(viewController.myTableView.numberOfRows(inSection: 0)).to(equal(3))
                }
            }
        }
    }
    
}
```
Now let go and run the test, You will see the result and how it is differ from XCTest.