Testing Rails on Ruby 1.9 using Rspec + Remarkable
By taylor luk on August 18, 2009 @ 10:00 AM
Background
When I started developing with Ruby on Rails last year and one of the remarkable thing I have found is the test-first cultural of the ruby community. There is a exhausted list of TDD/BDD/stubing/mocking libraries, sometime it's frustrating to get a good setup that is simple enough to maintain.
I consider myself as a newbie in TDD and instantly loved the syntax of Rspec and it's expressiveness, but dislike it's complexity. Finally, there it comes the Shoulda and find all the validation/association testing macros are really time savers.
As you can see, I stepped into Ruby 1.9 and find little information about a nice setup testing your ruby application in Ruby 1.9. Shoulda didn't work for me and found it wasn't Ruby 1.9 ready at that time (couple months ago) and things may(or not) have got better.
It forced me to look for alternative, and i discovered...
Remarkable
Remarkable is a framework for Rspec matchers that supports macro and internationalization. Remarkable-rails allowed Shoulda style testing in Rspec.
Those macros enables simple validation/association testing on your ActiveRecord models and It ran straight out of the box for Ruby 1.9.
It has remarkable taste
# It has your style. You can choose between:
it { should validate_numericality_of(:age).greater_than(18).only_integer }
it { should validate_numericality_of(:age, :greater_than => 18, :only_integer => true) }
should_validate_numericality_of :age, :greater_than => 18, :only_integer => true
should_validate_numericality_of :age do |m|
m.only_integer
m.greater_than 18
# Or: m.greater_than = 18
end
Rspec
In general Rspec works on Ruby 1.9 but it depends on a test library (test-unit) that isn't part of ruby 1.9 standard library anymore.
Stay with good old test-unit with this particular version that works, you need to remove any newer version you have. (hint: gem update)
gem install test-unit --VERSION=1.2.3
machinist
It's description speak for itself.
Fixtures aren't fun. Machinist is.
Remarkable setup for Rails
Finally I would like to share this remarkable setup that i used for many rails project. All gems are included as gem dependencies for test environment.
config/environments/test.rb
Testing stack
config.gem "rspec", :lib => false
config.gem "rspec-rails", :lib => false
config.gem "remarkable_activerecord", :lib => false
config.gem "remarkable_rails", :lib => false
config.gem "notahat-machinist", :lib => "machinist", :source =>"http://gems.github.com"
Install
RAILS_ENV=test rake gems:install
Conclusion
I find the experience testing in Ruby 1.9 is faster and more responsive. As a side note, however, I was actually planning to replace test-unit completely with minitest and gotten to a point it works perfectly for non-rails project, please let me know if you have solved it or have similar experience.
I hope you enjoy this post as part of Ruby 1.9 series i am writing.