Testing basics [4d]

Posted over 7 years ago. Visible to the public.


  • Understand why we test:
    • Low defect rate without a QA department.
    • Customer acceptance testing can concentrate on new features and things a robot cannot do (e.g. how does a feature look and feel).
    • Frequent deploy gets changes to users faster.
    • We sleep better, because we know stuff still works.
    • Make sure no one removes a feature by accident.
    • Ability to change one part of an application without needing to understand the entire system.
  • Why do we use different types of tests?
    • What are the pros and cons of unit tests?
    • What are the pros and cons of end-to-end tests ("E2E tests", "full-stack integration tests").
  • Learn basic RSpec for unit tests ("model specs")
  • Learn basic Cucumber Show archive.org snapshot for end-to-end tests
  • Learn to use Capybara Show archive.org snapshot to control a browser from your test script
  • Understand the disadvantages of too much testing.
    • Writing tests costs time.
    • Tests must be maintained when the application changes.
    • The more tests you already have, the less useful an additional test becomes ("Diminishing returns").
    • Tests sometimes fail even though the code is fine, especially when the test is coupled tightly to the implementation.
  • Learn how to run tests in existing applications.
  • Learn about Spreewald Show archive.org snapshot
  • 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 Show archive.org snapshot
  • Understand what "Test the behavior, not the implementation" means.



Play with existing tests

In one of the sample apps (like Deskbot):

  • Run the Rspec and the Cucumber tests. All tests should pass.
  • Find an RSpec example and break an expectation. See what happens when you re-run the test.
  • Find a Cucumber scenario and break an expection. See what happens when you re-run the test.

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 some of your custom step definitions with steps from Spreewald Show archive.org snapshot .
  • 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 Movie.search(query) 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 Movie.search() and the new UI.

Follow the advice from the "Testing" chapter from Growing Rails Applications in Practice Show archive.org snapshot :

  • Cucumber integration tests for the most common "happy path"
  • RSpec unit tests for all the edge cases (like case insensitivity)
Henning Koch
Last edit
4 months ago
Henning Koch
About this deck
We are makandra and do test-driven, agile Ruby on Rails software development.
Posted by Henning Koch to makandra Curriculum (2015-07-07 17:13)