Posted over 5 years ago. Visible to the public.
Testing basics [4d]
Goals
- 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.
- Diminishing returns:
- Learn how to run tests.
- Learn about Geordi's
rspec
,cucumber
andtests
commands
- Learn about Geordi's
- Learn about Spreewald
- 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
- You should be able to write both with "should" and "expect"
- You should know how to enable the old #should-syntax in RSpec 3
- Understand what "Test the behavior, not the implementation" means.
Resources
- Chapter "The value of tests" from Growing Rails Applications in Practice (in our library)
- Everyday Rails Testing with RSpec (in our library)
Concentrate on chapters 1 - 3 + 9.
Use a current version of RSpec during setup, at least 4.0.2 - Introduction to Writing Acceptance Tests with Cucumber
Exercises
Play with existing tests
- Let someone help you check out makandracards.com.
- 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 methodMovie.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 inMoviesController
- 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:
- Cucumber integration tests for the most common "happy path"
- RSpec unit tests for all the edge cases (like case insensitivity)