Posted over 2 years ago. Visible to the public.

Testing basics


  • Understand why we test:
    • QA without QA people
    • Continuous deployment
    • Sleep better, because we know stuff still works
    • Make sure no one removes a feature by accident
  • What is an unit test, what is an integration test?
  • Learn basic RSpec for unit tests
  • Learn basic Cucumber for integration tests
  • Learn about Capybara
  • Understand the disadvantages of too much testing
    • Diminishing returns:
      • Writing tests costs time
      • Diminishing returns: The more tests you already have, the less useful an additional test becomes.
  • Learn how to run tests.
    • Learn about Geordi's rspec, cucumber and tests commands
  • Learn about Spreewald
  • Learn when to write unit tests, when to write integration tests, when to write both
  • Understand why we rarely write controller specs or view specs
  • Understand the old RSpec "should" syntax
  • Understand what "Test the behavior, not the implementation" means.



Play with existing tests

  • Let someone help you check out
  • Run the rspec and the cucumber tests.
  • Find a spec and break it. See what happens.
  • Find a Cucumber feature and break it. See what happens.

Your first Cucumber feature

  • Write a Cucumber feature for CRUDing movies in your MovieDB application.
  • Write the feature once on your own, using Capybara to talk to the simulated browser. Make a commit.
  • Now remove your custom step definitions with steps from Spreewald.
  • Talk with your mentor about the pros, cons and limits of using canned steps.

Cucumber vs. RSpec

Add the following feature to MovieDB:

  • Above the movies list is a text field and a button "Search"
  • When the user enters text into the text field and hits "Search", the movies index is filtered to only contain matching movies

Some hints for the implementation:

  • Your Movie model should gain a class method Why should this not be an instance method?
  • You can implement the search in either SQL or Ruby
  • The form_for helper is not helpful for the search form above the index. Why is that so? What can you use instead?
  • Add a custom route /movies/search to your routes, and map it to a new #search action in MoviesController
  • Try to reuse some of the existing private methods for the implementation of #search
  • When rendering search results, try to reuse the existing index views, or at least large parts of its.

Now add tests for both the new method and the new UI.

Follow the advice from the "Testing" chapter from Growing Rails Applications in Practice:

  • Cucumber integration tests for the most common "happy path"
  • RSpec unit tests for all the edge cases (like case insensitivity)

Author of this card:

Henning Koch
Last edit:
about 1 year ago
by Tobias Kraze
Posted by Henning Koch to makandra Curriculum