Making TDD easier in Ruby by automating your tests
Reading time: 2 minsI’m a fan of test driven development, in order to speed up my turnaround time from writing functionality to testing the functionality, I use Guard(https://github.com/guard/guard) tool to automatically run my test any time I make a change.
This greatly speeds up my development time and hopefully it’ll help you too.
This is going to be our final files and folder structure.
Gemfile
Gemfile.lock
Guardfile
lib
spec
Dependencies Setup
First step is to setup a gem file with the dependencies that we will be using.
Create a new file named ‘Gemfile’ in your main project directory with the following contents
#Gemfile
source "https://rubygems.org"
gem "rspec"
group :development do
gem 'guard'
gem 'guard-rspec'
end
Once done, run the bundle install commind in the terminal to install the dependencies, like so.
bundle install
Seting up Guard
Create a new file named ‘Guardfile’ with the following content
#Guardfile
guard :rspec, cmd: "bundle exec rspec --color" do
#Guardfile
# watch /lib/ files
watch(%r{^lib/(.+).rb$}) do |m|
"spec/#{m[1]}_spec.rb"
end
# watch /spec/ files
watch(%r{^spec/(.+).rb$}) do |m|
"spec/#{m[1]}.rb"
end
end
This file configures the Guardfile tool to watch for any changes to ruby files within the lib and spec directories and runs rspec.
Note: We have taken this route of manually creating a Guardfile rather than running “guard init” so we more easily see what the guard file is doing. Guard init generates a lot of code that we won’t be using in this guide.
Finally we launch our Guard instance to start watching our files by running the following command in the terminal.
guard
That’s it, hopefully this helps speeds up your development time.
Here is the completed repo.
Cedric