SWIFTLY TESTING WITH QUICK

Who am i?



Goals of this talk



  • Learn what testing is and why you should test.
  • Understand how to implement Test-Driven Development in to your every day work.
  • Learn how to leverage Quick and Nimble to write good tests.

What is a test?


What is a UNIT test?





A unit test is a unit of code that verifies the functionality or behavior of a unit of your system (application in our case).




There are multiple types of tests.

What DOES a test Look Like?


Example: Let's say we have a function addNumbers that takes two integers a and b as input. We expect that when we provide the input 1 and 5, the output of our function would be 6.


WHAT DOES A TEST LOOK LIKE?





What is test-driven development?

What is test-driven development?



  • Write a failing test.
  • Write the minimum amount of code to make our test pass.
  • Refactor.
  • Repeat.
  • TDD for short



Pros of tDD


  • Better code quality (less coupling and higher cohesion).
  • Ability to refactor without fear of breaking things.
  • Increased protection from defects such as regressions.
  • Tests provide documentation for expected functionality.
  • Fast feedback loop.
  • Increased confidence.

CONS OF TDD



  • Steep learning curve (different tools/methodologies).
  • Increased setup costs.
  • Slower feature development due to writing associated tests.
  • Increased cost of test maintenance as projects progress.

What we will be building


We will be building a simple Bank Account. 

Our Bank Account will have the following functionality

  • Deposit money
  • Withdrawal money
  • Apply overdraft fee

What is quick and nimble?




Quick is a BDD (Behavior-driven development) testing framework for Swift and Objective-C.

Nimble is a Matcher framework for Swift.

Installing quick and nimble

Clone Quick and Nimble

git clone https://github.com/Quick/Quick.git
git clone https://github.com/Quick/Nimble.git

installing quick and nimble

Add Quick.xcodeproj and Nimble.xcodeproj to your test target


Right-click on the group containing your application's tests and select "Add Files to *Your Application Name*"

INstalling quick and nimble

Select Quick.xcodeproj from the path in which you cloned it.
Follow the same steps for Nimble.xcodeproj

Installing quick and nimble


Link Quick.framework and Nimble.framework

WRiting our first test



Watching our test fail



Getting to know xcode and testing

  • From Test Navigator (CMD + 5)
    • Click on the play button next to our BankAccountSpec.
    • Right click on the test and choose Test *Test name*. Same for BankAccountSpec. Test "BankAccount Spec"

GETTING TO KNOW XCODE AND TESTING


  • From the code
    • Click the play button next to our class definition.



  • CTRL + CMD + U
    • Will test without building
  • SHIFT + CMD + U
    • Will test with building

GETTING TO KNOW XCODE AND TESTING

What happens then there is an error?

  • XCode will tell us a test failed.
  • In the Test Navigator we will see an X next to the failing test.
  • In the code there will be an X next to the failing expectation. With the expect x, but got y.

You can also see the expectation in the Issue Navigator (CMD + 4)

implementing the bank account class



IMPLEMENTING THE BANK ACCOUNT CLASS




What we have learned so far


  • What a unit test is.
  • What Test-Driven Development or TDD is.
  • How to install Quick and Nimble.
  • How to write our first test.
  • How to run and view our tests results.
  • How to happy dance.

GETTING TO know nimble

  • to - Expect it to match.
  • toNot - Expect it to not match.
  • beCloseTo(expectedValue, within: Double = 0.0001)
  • beLessThan(upperLimit)
  • beGreaterThan(upperLimit)
  • be(Less|Greater)ThanOrEqualTo(lowerLimit)
  • raiseException()
  • beNil()
  • beTruthy() - That it's true
  • beFalsy()


GETTING TO KNOW NIMBLE

  • contains(items: T...)
    • Swift Collections that have Equatable elements
    • NSArrays and NSSets
    • Strings - Uses substring matching
  • (begin|end)With(starting: T)
    • Swift Collections that have Equatable elements
    • NSArrays
    • Strings - which uses substring matching
  • endsWith(ending: T)
    • Same as above
  • beEmpty()
    • Works with Strings and Collections from Swift + Obj-C

    GETTING TO KNOW NIMBLE

    New Awesomesauce! Operator Overload!

    • == instead of .to(equal(expected))
    • != instead of .toNot(equal(expected))
    • === instead of .to(beIdenticalTo(expected))
    • !=== instead of .toNot(beIdenticalTo(expected))
    • > instead of beGreaterThan
    • < instead of beLessThan
    • >= and <= be(Greater|Less)Than

    GETTING TO KNOW NIMBLE

    Other things you can do with Nimble

    • Create your own matchers.
    • Have Async Expectations.
      • Replace to and toNot with toEventually and toEventuallyNot.
      • Polls the expression inside expect until the given expectation succeeds within a second or given timeout.

    WRITE MORE TESTS!



    WRITE MORE TESTS!

    WRITE MORE TESTS!


    WRITE MORE TESTS!


    IMPLementing withdrawal


    IMPLEMENTING WITHDRAWAL




    WHAT IS DESCRIBE?

    Describe allows us to wrap a set of tests against one bit of functionality.

    In our case, our method #withdrawal.

    We use # to indicate an instance methods name.

    We use . when indicating a class methods name.

    This is the style I adopted from Ruby/RSpec best practices.


    IMPLEMENTING WITHDRAWAL




    IMPLEMENTING WITHDRAWAL




    What happens if we OVERDRAW?

    IMPLEMENTING THE OVERDRAFT FEE

    WHAT IS CONTEXT?




    We use context to specify conditional behavior

    with positive balance.
    with negative balance.

    IMPLEMENTING THE OVERDRAFT FEE



    REFACTOR!


    Move fee applying functionality to it's own method.
    This allows us to give this bit of functionality a name.

    REFACTOR!


    Refactor!


    REfactoring our tests!


    REFACTORING OUR TESTS!


    We can share setup code with beforeEach
    There's also afterEach (runs after each test), afterSuite, and beforeSuite.

    that's it for now


    Follow @adamjleonard 
    http://www.adamjleonard.com

    Subscribe to
    http://www.swifcast.tv

    Add Swift libraries to
    http://www.swifttoolbox.io

    Get your Social Media Marketing
    http://blog.iscreamsocial.io

    Swiftly testing with quick

    By Adam Leonard

    Swiftly testing with quick

    • 3,243