Slide 1

Slide 1 text

Testing in Ruby

Slide 2

Slide 2 text

I’m Darcy

Slide 3

Slide 3 text

Fellow student here

Slide 4

Slide 4 text

I make APIs for iOS Apps in Ruby

Slide 5

Slide 5 text

Q: What is testing?

Slide 6

Slide 6 text

Automated Testing

Slide 7

Slide 7 text

Verifiable contracts

Slide 8

Slide 8 text

Given this, I expect that.

Slide 9

Slide 9 text

“Test Suites”

Slide 10

Slide 10 text

Q: Why test?

Slide 11

Slide 11 text

Testing as a contract

Slide 12

Slide 12 text

Prevent regressions.

Slide 13

Slide 13 text

Verify your assumptions

Slide 14

Slide 14 text

Confirm you’re understanding

Slide 15

Slide 15 text

Things you forget.

Slide 16

Slide 16 text

Real World Example: Merging data.

Slide 17

Slide 17 text

Testing as a design tool

Slide 18

Slide 18 text

“T.D.D.”

Slide 19

Slide 19 text

Test-first development

Slide 20

Slide 20 text

Red Green Refactor

Slide 21

Slide 21 text

Write a failing test

Slide 22

Slide 22 text

require 'test_helper' class PersonTest < ActiveSupport::TestCase test "making a full name" do person = Person.new first_name: "Bob", last_name: "Smith" assert_equal "Bob Smith", person.full_name end end

Slide 23

Slide 23 text

# Running tests: E Finished tests in 0.057601s, 17.3608 tests/s, 0.0000 assertions/s. 1) Error: PersonTest#test_making_a_full_name: NoMethodError: undefined method `full_name' for # test/models/person_test.rb:7:in `block in ' 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips ==============================================================================

Slide 24

Slide 24 text

Make it pass

Slide 25

Slide 25 text

class Person < ActiveRecord::Base def full_name first_name + " " + last_name end end

Slide 26

Slide 26 text

# Running tests: . Finished tests in 0.132392s, 7.5533 tests/s, 7.5533 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips ==============================================================================

Slide 27

Slide 27 text

Refactor it.

Slide 28

Slide 28 text

class Person < ActiveRecord::Base def full_name "#{first_name} #{last_name}".strip end end

Slide 29

Slide 29 text

# Running tests: . Finished tests in 0.177100s, 5.6465 tests/s, 5.6465 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips ==============================================================================

Slide 30

Slide 30 text

We’re all human.

Slide 31

Slide 31 text

Work out what you want

Slide 32

Slide 32 text

Write as little as possible to make it happen

Slide 33

Slide 33 text

Work out what you don’t know

Slide 34

Slide 34 text

Testing as documentation

Slide 35

Slide 35 text

Testing in Rails

Slide 36

Slide 36 text

Rails <3’s testing

Slide 37

Slide 37 text

Testing is a standard part of Rails

Slide 38

Slide 38 text

$ rake

Slide 39

Slide 39 text

Run options: --seed 13019 # Running tests: ...FE Finished tests in 0.077580s, 64.4496 tests/s, 51.5597 assertions/s. 1) Failure: PersonTest#test_making_a_name_with_no_given_names [/Users/sutto/Code/test_project/test/models/person_test.rb:22]: Expected: nil Actual: "" 2) Error: PersonTest#test_making_searching_for_users: NoMethodError: undefined method `search' for # test/models/person_test.rb:28:in `block in ' 5 tests, 4 assertions, 1 failures, 1 errors, 0 skips ============================================================================== Run options: --seed 5103 # Running tests: . Finished tests in 0.086914s, 11.5056 tests/s, 11.5056 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips ==============================================================================

Slide 40

Slide 40 text

Generates automatically generate stubs for you.

Slide 41

Slide 41 text

xUnit

Slide 42

Slide 42 text

JUnit

Slide 43

Slide 43 text

Test::Unit

Slide 44

Slide 44 text

MiniTest

Slide 45

Slide 45 text

Assertions

Slide 46

Slide 46 text

assert

Slide 47

Slide 47 text

assert_equal 2, (1 + 1)

Slide 48

Slide 48 text

Unit / Model Tests

Slide 49

Slide 49 text

Small, independent bits of functionality.

Slide 50

Slide 50 text

Minimal set of dependencies

Slide 51

Slide 51 text

test "making a name with first and last name" do person = Person.new first_name: "Bob", last_name: "Smith" assert_equal "Bob Smith", person.full_name end test "making a name with first name only" do person = Person.new first_name: "Bob" assert_equal "Bob", person.full_name end test "making a name with last name only" do person = Person.new last_name: "Smith" assert_equal "Smith", person.full_name end test "making a name with no given names" do person = Person.new assert_equal nil, person.full_name end

Slide 52

Slide 52 text

Test edge cases

Slide 53

Slide 53 text

Small, fast and easy to test assumptions.

Slide 54

Slide 54 text

test "making searching for users" do person = Person.create! first_name: "Bob", last_name: "Smith" # Actually hit the database and get a result... Person.search('Bob Smith').should == [person] end

Slide 55

Slide 55 text

Functional / Controller Tests

Slide 56

Slide 56 text

Test how the model, view and controller interact

Slide 57

Slide 57 text

require 'test_helper' class PeopleControllerTest < ActionController::TestCase test "should list users" do person = Person.create! get :index assert_equal [person], assigns(:people) end end

Slide 58

Slide 58 text

test "redirecting when not logged in" do get :index assert_response :redirect assert_redirected_to some_other_page end test "visible when logged in" do get :index assert_response :success end

Slide 59

Slide 59 text

Guarantees*.

Slide 60

Slide 60 text

* Only test what you specify, nothing more.

Slide 61

Slide 61 text

Checks the glue.

Slide 62

Slide 62 text

Integration tests.

Slide 63

Slide 63 text

The bigger picture

Slide 64

Slide 64 text

E.g. user stories

Slide 65

Slide 65 text

User logs in, purchases an item, order is placed.

Slide 66

Slide 66 text

Slower to run, but test everything together.

Slide 67

Slide 67 text

Taking it Further

Slide 68

Slide 68 text

Full Stack Tests

Slide 69

Slide 69 text

Coverage Tools

Slide 70

Slide 70 text

Simplecov

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

See code that isn’t tested

Slide 73

Slide 73 text

BDD

Slide 74

Slide 74 text

RSpec

Slide 75

Slide 75 text

minitest-spec

Slide 76

Slide 76 text

What I’ve learnt about testing

Slide 77

Slide 77 text

Over testing is bad.

Slide 78

Slide 78 text

Slow tests sucks.

Slide 79

Slide 79 text

Testing should be instinctive

Slide 80

Slide 80 text

Don’t get stuck on numbers (coverage, number of tests)

Slide 81

Slide 81 text

Good tests inspire confidence

Slide 82

Slide 82 text

Only test what you implement

Slide 83

Slide 83 text

If you have to be told to test, it’s probably not going to help

Slide 84

Slide 84 text

It takes time to learn to write good tests

Slide 85

Slide 85 text

Don’t be afraid to write tests before code

Slide 86

Slide 86 text

Most importantly, writing tests costs less long term.