Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TDD Workshop - UIKonf 2016

TDD Workshop - UIKonf 2016

Pawel Dudek

May 25, 2016
Tweet

More Decks by Pawel Dudek

Other Decks in Programming

Transcript

  1. Test Driven
    Development

    View Slide

  2. Why do we need tests?

    View Slide

  3. Preserving behaviors

    View Slide

  4. We're just humans

    View Slide

  5. Failsafe

    View Slide

  6. Test Driven
    Development

    View Slide

  7. Tests drive the way you
    code

    View Slide

  8. Always write test first

    View Slide

  9. Tests influence
    architecture of your
    app

    View Slide

  10. Tests tell you whether
    your design became
    too complex

    View Slide

  11. Have to simulate
    behavior of your
    dependency
    dependency
    dependency?

    View Slide

  12. Need to fake seven
    objects?

    View Slide

  13. Need to call five
    functions to simulate
    a behavior?

    View Slide

  14. Hard to specify clear
    requirements?

    View Slide

  15. Overcomplicated
    design

    View Slide

  16. Thinked-through
    design

    View Slide

  17. First consumer of your
    API

    View Slide

  18. Programs must be written for people
    to read, and only incidentally for
    machines to execute.
    — Harold Abelson

    View Slide

  19. There is no such thing
    as untestable behavior
    Only untestable code

    View Slide

  20. What is testable code?

    View Slide

  21. Good Architecture
    ==
    Testable code

    View Slide

  22. What is good
    architecture?

    View Slide

  23. Cohesion

    View Slide

  24. Coupling

    View Slide

  25. Good design
    A simple definition

    View Slide

  26. High cohesion
    and
    low coupling

    View Slide

  27. Easily composable
    and
    context independent

    View Slide

  28. Good architecture - base rules
    » Single responsibility
    » Few dependencies
    » Depend on interfaces, not classes (yay POP!)

    View Slide

  29. View Slide

  30. !!!

    View Slide

  31. http://www.martinfowler.com/
    bliki/
    DesignStaminaHypothesis.html

    View Slide

  32. SOLID

    View Slide

  33. SOLID
    Homework
    » Goruco 2009 - SOLID Object-Oriented Design - Sandi Metz
    » MCE^3 - Software Paradigms & Patterns — Did We Get It All
    Wrong? - Jon Reid
    » MCE^3 - Jorge Ortiz
    » Clean Architecture - Uncle Bob

    View Slide

  34. What is BDD

    View Slide

  35. Behavior Driven Development
    Test Driven Development

    View Slide

  36. What's the difference?

    View Slide

  37. BDD aims to improve
    certain aspect of TDD

    View Slide

  38. BDD tries to help you
    know what to test

    View Slide

  39. When writing tests
    don’t think ‘tests’

    View Slide

  40. Think about
    ‘behaviors’

    View Slide

  41. Think about examples
    how your object
    should behave

    View Slide

  42. An objects behavior is
    defined by methods it
    declares in its
    interface

    View Slide

  43. You should not be testing internal
    implementation of your object
    Only its interface

    View Slide

  44. Work outside-in

    View Slide

  45. Ubiquitous language

    View Slide

  46. Quick & Nimble

    View Slide

  47. class DolphinSpec: QuickSpec {
    override func spec() {
    it("is friendly") {
    expect(Dolphin().isFriendly).to(beTruthy())
    }
    it("is smart") {
    expect(Dolphin().isSmart).to(beTruthy())
    }
    }
    }

    View Slide

  48. describe("a dolphin") {
    describe("its click") {
    it("is loud") {
    let click = Dolphin().click()
    expect(click.isLoud).to(beTruthy())
    }
    it("has a high frequency") {
    let click = Dolphin().click()
    expect(click.hasHighFrequency).to(beTruthy())
    }
    }
    }

    View Slide

  49. describe("a dolphin") {
    var dolphin: Dolphin!
    beforeEach { dolphin = Dolphin() }
    describe("its click") {
    var click: Click!
    beforeEach { click = dolphin.click() }
    it("is loud") {
    expect(click.isLoud).to(beTruthy())
    }
    it("has a high frequency") {
    expect(click.hasHighFrequency).to(beTruthy())
    }
    }
    }

    View Slide

  50. describe("its click") {
    context("when the dolphin is not near anything interesting") {
    it("is only emitted once") {
    expect(dolphin!.click().count).to(equal(1))
    }
    }
    context("when the dolphin is near something interesting") {
    beforeEach {
    let ship = SunkenShip()
    Jamaica.dolphinCove.add(ship)
    Jamaica.dolphinCove.add(dolphin)
    }
    it("is emitted three times") {
    expect(dolphin.click().count).to(equal(3))
    }
    }
    }

    View Slide

  51. That's about it!
    Let's dive into code!

    View Slide

  52. Assignment 0
    » Add data parsing in ChatMessagesUpdater

    View Slide

  53. Assignment 1
    » Pass data from model object to table view cell

    View Slide

  54. That's all folks!

    View Slide