Automation test with Guard and Rubymine integration.

You may already know about `rspec` and `rubocop`, a testing tool and a checking tool (for ruby coding styles). They are not realted to be referred here in a same post but we’re using them these days in our company projects though. And actually we’re going to play with automation tests using `guard` and its plugins today, which are `guard-rspec` and `guard-rubocop`.

Rubocop and RSpec

For you who haven’t heard about `rubocop` and `rspec`  have a look here:

  • https://github.com/bbatsov/rubocop
  • https://github.com/rspec/rspec-rails
  • http://rspec.info/

Automation

`guard` helps us having things rerun automatically when new changes get saved. `guard-rspec` gets a spec re-run when you change and save it, `rubocop` gets fired by `guard-rubocorp` immediately after any of your files got saved (of course only the ones in the rubocop.yml configuration list), specs get run by `guard-rspec` when any of your spec gets changed and saved.

Note: One interesting thing is `guard-rspec` re-runs only the spec recently changed, not the whole spec directory.

Firstly make sure you have `guard` gem installed. Then add `guard-rspec` and `guard-rubocop` to the Gemfile:

group :development do
  gem 'guard-rspec', require: false
  gem 'guard-rubocop'
end

Secondly let’s generate the `Guardfile` which contains the configurations.

guard init

or by

guard init rubocop
guard init rspec

Lastly, open another console and type in `guard`

gnguyen@gnguyen-ubuntu ~/work/bookstore (master)*$ guard

We now have specs and files automatically rerun/re-checked whenever new changes get saved 🙂

RubyMine integration

Once we have both Guard running and Rubymine installed,  we can make them work together seamlessly with just a few more steps.

Step 1: Enable “Save files on frame deactivation”

Open the project directory with Rubymine, go to File > Settings then type save in the Search box, make sure the checkbox for Save files on frame deactivation is checked.
01-activate-save-files-on-frame-deactivation

 

 

Step 2: Add Guard configuration

2-configuration

3-bundler

  • Go to Run > Edit Configuration
  •  Add new IRB concolse by clicking the + sign
  •  Name the new IRB console as ‘guard’ (or whatever you like)
  •  Set the Guard path in the IRB script field. We can retrieve this path by opening a new Terminal, navigate to the project directory and type which guard
gnguyen@ubuntu ~/my_project$ which guard
/home/gnguyen/.rvm/gems/ruby-2.0.0-p195@my_project/bin/guard

That’s it, go to Run > Run "guard"  and see rubocop and rspec run when we save new changes.

4-success

Have fun.

Add a Comment

Scroll Up