Posted about 6 years ago. Visible to the public.

Testing basics [4d]


  • 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 Archive for integration tests
  • Learn about Capybara Archive
  • 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 Spreewald Archive
  • Learn when to write unit tests, when to write integration tests, when to write both
  • Understand why we rarely write request specs, controller specs or view specs
  • Understand the old RSpec "should" syntax Archive
  • 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 Archive .
  • 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 or even better a scope method you can apply to the scope in the controller. 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 Archive :

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

Owner of this card:

Henning Koch
Last edit:
7 months ago
by Klaus Weidinger
Posted by Henning Koch to makandra Curriculum
This website uses short-lived cookies to improve usability.
Accept or learn more