Guard Your TDD
Nowadays, Test Driven Development (TDD) is very opinionated (some might even think that TDD is dead) but it is hard to argue that it has become a standard of how we write applications.
Unfortunately, a lot of developers miss the core idea of TDD, namely, that the tests have to be written first and implementation is kind of a side effect which makes the tests pass, not the other way around.
In order to do TDD efficiently, we need to setup a feedback loop from our tests so we can follow the proper workflow.
After installing gems and bootstrapping
$ bundle exec guard init rspec
We can tweak our newly created
Guardfile a bit.
cmd to run your tests to one using bin stubs to take advantage of using
spring and execute your tests faster.
keep option of
failed_mode to make
guard remember failed specs which were executed. Guard will then add those to run list and keep executing until they pass.
3) Add option to clean a terminal screen after each tests execution to always see the output from the last run.
guard :rspec, cmd: "./bin/rspec", failed_mode: :keep do ... clearing :on ... end
With such simple setup, we have created a constant feedback loop from our tests. From now, on any change in code will trigger related specs: for example, a change in
models/user.rb will trigger
spec/models/user_spec.rb automatically so we can see the output from tests execution without even switching terminals.