TESTUNIT + GUARD + SPORK + GROWL
It is a cumbersome job to run your tests manually every single time you want to check wether your tests are doing what you intended to do, or are they failing. So I wanted to find out how I can automate it.
Spork is a server which is meant for your tests. So that means spark forks a copy of the server each time you run your tests instead of unloading every single time when you run them. This makes the time that you spend on running your tests much shorter. You can find more information about spork from this link http://rubydoc.info/gems/spork/0.9.2/frames
Guard is a command line tool to easily handle events on file system modifications. Guard supports FSEvent on Mac OS X, Inotify on Linux, and Directory Change Notification on Windows. You can also poll on other operating systems and fast file modification detection when polling is not used. Guard supports visual system notifications too. All on all it is awesome.
First of all I am using Mac thats the reason I am also using Growl. But feel free to search for Notification systems. So as I said, Growl is a notification system for Mac OS X. It allows applications to send you notifications in certain events. So most commonly you will see a bubble on the right top corner of your screen with some information regarding your application.
Unit testing as it is stated in its name is teasing the smallest part of your code. TestUnit comes by default when you create a rails application and find it in your "test" folder. So no need to explain in detail. If you would like to have more information about unit testing, you can check this link: http://en.wikipedia.org/wiki/Unit_testing
FIrst we need to modify our gem file a little.
Add those lines to your gem file
group :test do gem 'spork' #Spork gem gem 'spork-testunit' #test unit support gem 'guard-spork' #guard support gem 'growl' #for growl notifications gem 'guard-minitest' #guard minitest support end
When you are done installing the gems run the following:
bundle exec spork --bootstrap
bundle exec guard init spork
bundle exec guard init test
Now when we are done creating our files and stuff lets make some modifications.
Make your file look like this. here we have added a cucumber and rspec support besides test_unit but you can get rid of them if you like.
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit_env => { 'RAILS_ENV' => 'test' } do watch('config/application.rb') watch('config/environment.rb') watch(%r{^config/environments/.+\.rb$}) watch(%r{^config/initializers/.+\.rb$}) watch('Gemfile') watch('Gemfile.lock') watch('spec/spec_helper.rb') { :rspec } watch('test/test_helper.rb') { :test_unit } watch(%r{features/support/}) { :cucumber } end guard 'minitest', do # with Minitest::Unit watch(%r|^test/unit/(.*)\.rb|) ### DO NOT FORGET TO ADD THIS LINE watch(%r|^test/(.*)\/?test_(.*)\.rb|) watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" } watch(%r|^test/test_helper\.rb|) { "test" } end
Go to your test folder and open test_helper.rb file. You should see some things that spork has been adding which we need to modify to make it work a little better. So your file should look something like below after you modify it.
require 'rubygems' require 'spork' #uncomment the following line to use spork with the debugger #require 'spork/ext/ruby-debug' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end end Spork.each_run do # This code will be run each time you run your specs. end
So after all the configuration are done and we are ready to go wrote all of our tests and stuff just run the following command;
and you should start seeing some notifications :)