image by Pascal Meier
Rails comes with a built-in testing framework and many Rubyists evangelise various methods of testing: Behaviour Driven Development, 100% test coverage, Red-Green-Refactor.
But perhaps you’ve never really ‘got it’ or ever seen the benefits of an established testing culture. As a result you might not have any tests in your Rails application.
…not having any tests:
…just write one “smoke” test.
class HomePageTest < ApplicationSystemTestCase test "show homepage" do visit "/" assert_text "Text on your homepage" end end
You can run this test by typing
bundle exec rails test:system.
This single test does two major things.
One, because it is a ‘system test’ using a real browser to test your entire site, it checks that your application builds, runs successfully and displays a homepage to the world. This has value.
Two, now you’ve taken your first step into testing, you can take more.
For me, it’s a case of: why stop here? Once you have this basic smoke test, you can add a simple test to check that sign up of new users works. Or that signing in an existing user works.
These sorts of simple, but full system, tests will give you a surprising amount of benefit for very little ongoing effort.
These kinds of system tests rarely allow you to test all the permutations of sophisticated logic within your application. Those sorts of things are better tested in unit tests. For general “is my application mostly working”-type concerns, you’ll get a lot of milage (and coverage) out of these broad smoke tests.
Last updated on November 17th, 2019 by @andycroll
An email newsletter, with one Ruby/Rails technique delivered with a ‘why?’ and a ‘how?’ every two weeks. It’s deliberately brief, focussed & opinionated.