I am learning Rails 3 @ Ruby on Rails Tutorial by Michael Hartl. A very nice and detail tutorial with many examples for beginners. Highly recommended.
But i got some problems when i tried to setup the Autotest for the RSpec framework. Autotest is a continuous testing tool which run the test suite automatically based on the files you have changed. RSpec is a Behavior Driven Development (BDD) framework for Ruby. After a few hours of struggling, i finally made it work.
1. Create a new Rails project without the default Test::Unit framework.
- rails new sample_app -T
2. Edit the project Gemfile as follow
source 'http://rubygems.org' gem 'rails', '3.0.1' gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3' group :development do gem 'rspec-rails', '2.2.1' end group :test do gem 'rspec', '2.2.0' gem 'webrat', '0.7.2' gem 'autotest', '4.4.4' gem 'redgreen', '1.2.2' gem 'test-unit', '2.1.1' end
3. Install the dependencies which is listed in the Gemfile
- bundle install
4. Create the files of RSpec
- rails generate rspec:install
5. Create the Pages controller with 2 views named as home and contact
- rails generate controller Pages home contact
6. Remove the view and helper tests which we don’t need in the example.
- rm -rf spec/views
- rm -rf spec/helpers
7. Run the test cases in the spec folder to check if RSpec could be run without problem
- rspec spec/
8. We can now proceed to the Autotest setup. Install the libnotify-bin for showing the popup about the test result in GNOME.
- sudo apt-get install libnotify-bin
9. Create the Autotest configuration file under the user home directory.
~/.autotest
#!/bin/ruby gem 'redgreen' require 'autotest/timestamp' module Autotest::GnomeNotify def self.notify title, msg, img system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000" end Autotest.add_hook :ran_command do |at| image_root = "~/.autotest_images" results = [at.results].flatten.join("\n") results.gsub!(/\\e\[\d+m/,'') output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/) full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i) if output if failures > 0 notify "FAIL", "#{output}", "#{image_root}/fail.png" elsif pending > 0 notify "Pending", "#{output}", "#{image_root}/pending.png" else notify "Pass", "#{output}", "#{image_root}/pass.png" end end end end
10. Also create a ~/.autotest_images folder and save the following 3 pictures inside it.
11. Run the Autotest now
- bundle exec autotest
12. A pop up message is promoted whenever the tests are completed. but i don’t know how to fix the warning as highlighted in the following picture before the test started.
*The above warning is fixed if the Zentest gem does not exist. (Update @ 2010-11-19)
There is an alternative way to setup the Autotest by using the autotest-notification gem. Please refer to Growl style images for autotest-notification using Ubuntu 10.04 for more information.
Done and enjoy the Ruby and Rails. =)
Update @ 2010-11-19
- Remove the installations of ZenTest and Redgreen gems. Actually the required gems will be installed when running bundle install. Moreover, the warning during the Autotest run is fixed if the ZenTest gem does not exist. Thanks bunwich very much for finding the solution.
- For Fedora user, install libnotify instead of libnotify-bin. Thanks bunwich again. =)
- Updated the Autotest version in Gemfile from 4.4.2 to 4.4.4.
Update @ 2010-12-02
Update the Gem version to solve the Failure/Error: Unable to find matching line from backtrace stack level too deep error when running rspec test.
- rspec-rails – from 2.1.0 to 2.2.1
- rspec – from 2.1.0 to 2.2.0
If you want to remain using version 2.1.0, then change the webrat version from 0.7.2 to 0.7.1.
Reference:
I removed zentest and the warnings disappeared.
Thanks worked well on fedora 14. One exception was on fedora libnotify-bin is just called libnotify.
Thanks.
LikeLike
Thanks bunwich. I have updated the post about what u found. =)
LikeLike
Awesome explanation!!! I am going thru the same tutorial book and had to struggle with tests for a while too.
Thanks a lot!
LikeLike
you are welcome =)
LikeLike
Thanks very much. Your tutorial worked perfectly.
LikeLike
Glad to know that it could help =D
LikeLike
Nice one. First time I ever managed to get this working. Thanks.
LikeLike
Great! =D
LikeLike
Worked great, thanks a ton! Go Ubuntu!
LikeLike
good to know that it could help~ =D
LikeLike