Last Updated: February 25, 2016
·
561
· jacaetevha

Focused BDD with Guard

Guard is great. RSpec is great.

What's not great? Firing up Guard, waiting several minutes until all specs pass and I can start focusing on my current spec.

What else is not so great? BDD'ing my way through a solution, get a failing spec, making it pass, immediately waiting another few minutes while the entire suite is run again.

If your spec suites aren't very long then you might not care so much. Anything more than 20 seconds makes me antsy when I'm doing BDD/TDD.

So, what to do? With guard-rspec there are several flags you can set to help you go faster. Namely, :all_on_start, :all_after_pass, :focus_on_failed.

But, those settings alone are not good enough for me. I sometimes want to run the whole suite on every change, just not when I'm BDD'ing a solution. So, what I do is leverage Guard's groups to run the suite in different "modes".

Here's my (snipped) Guardfile. Note that I also use Spork (thus the --drb in the --cli option). Also, I sometimes want to fail fast, and sometimes not, so instead of changing Guard all the time I will conditionally include the --fail-fast option in the --cli option.

# reuse this block in each guard-rspec invocation
rspec_block = lambda do
  # ... watchers for specs
end

group :focused_specs do
  guard 'rspec', :all_on_start => false, :all_after_pass => false, :focus_on_failed => true, :cli => "--color #{ENV['FF'] ? '--fail-fast' : ''} --drb", &rspec_block
end

group :tests do
  guard 'rspec', :cli => "--color #{ENV['FF'] ? '--fail-fast' : ''} --drb", &rspec_block

  # other non-RSpec tests
end