Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It’s fully compatible with your existing tests, and requires no retooling to use.
- Helpers
#context and #should give you RSpec like test blocks. In addition, you get nested contexts and a much more readable syntax.
- Macros
Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros. They get you started quickly, and can help you ensure that your application is conforming to best practices.
- Assertions
Many common rails testing idioms have been distilled into a set of useful assertions.
- Matchers
Rspec-compatible matchers providing the same tests as Shoulda macros.
Stop killing your fingers with all of those underscores… Name your tests with plain sentences!
classUserTest<Test::Unit::TestCasecontext"A User instance"dosetupdo@user = User.find(:first) endshould"return its full name"doassert_equal'John Doe', @user.full_nameendcontext"with a profile"dosetupdo@user.profile = Profile.find(:first) endshould"return true when sent #has_profile?"doassert@user.has_profile?endendendend
Produces the following test methods:
"test: A User instance should return its full name.""test: A User instance with a profile should return true when sent #has_profile?."
So readable!
Quick macro tests for your ActiveRecord associations and validations:
classPostTest<Test::Unit::TestCasefixtures:allshould_belong_to:usershould_have_many:tags, :through=>:taggingsshould_validate_uniqueness_of:titleshould_validate_presence_of:body, :message=>/wtf/should_validate_presence_of:titleshould_validate_numericality_of:user_idendclassUserTest<Test::Unit::TestCaseshould_have_many:postsshould_not_allow_values_for:email, "blah", "b lah"should_allow_values_for:email, "a@b.com", "asdf@asdf.com"should_ensure_length_in_range:email, 1..100should_ensure_value_in_range:age, 1..100should_not_allow_mass_assignment_of:passwordend
Makes TDD so much easier.
Macros to test the most common controller patterns…
classPostsControllerTest<ActionController::TestCasecontext"on GET to :show for first record"dosetupdoget:show, :id=>1endshould_assign_to:usershould_respond_with:successshould_render_template:showshould_not_set_the_flashshould"do something else really cool"doassert_equal1, assigns(:user).idendendend
More to come here, but have fun with what’s there.
assert_same_elements([:a, :b, :c], [:c, :a, :b]) assert_contains(['a', '1'], /\d/) assert_contains(['a', '1'], 'a')
Any *.rb file under RAILS_ROOT/test/shoulda_macros/ or vendor/(plugins|gems)/gem_name/shoulda_macros/ will be automatically required when you run your tests. This allows you to distribute macros with your plugins, or to organize the macros inside your application. Remember to add your macro to Test::Unit::TestCase in the macro file:
# test/shoulda_macros/security.rbclassTest::Unit::TestCasedefself.should_be_denied(opts = {}) should_set_the_flash_to(opts[:flash] ||/Please log in/i) should_redirect_to(opts[:redirect] ||'login_url') endend
Use this if you prefer to use versioned releases of shoulda. Specify the gem dependency in your config/environment.rb file:
Rails::Initializer.rundo|config|config.gem"thoughtbot-shoulda", :lib=>"shoulda", :source=>"http://gems.github.com"end
Then:
$ rake gems:install $ rake gems:unpack
Use this if you prefer to use the edge version of shoulda:
$ script/plugin install git://github.com/thoughtbot/shoulda.git
Use this if you prefer the idea of being able to easily switch between using edge or a tagged version of shoulda:
$ git submodule add git://github.com/thoughtbot/shoulda.git vendor/plugins/shoulda
If you’re using Shoulda with RSpec, we recommend that you add config.gem lines for RSpec and Shoulda in your config/environment/test.rb file, but do not ask Rails to load the RSpec and Shoulda libraries:
config.gem'rspec', :lib=>falseconfig.gem'rspec-rails', :lib=>falseconfig.gem'thoughtbot-shoulda', :lib=>false, :source=>'http://gems.github.com'
Then require shoulda from your spec/spec_helper.rb file, before Spec::Runner is configured:
# requires for RSpec require 'shoulda' Spec::Runner.configure do |config| # ...
You should not need to require anything besides the top-level shoulda library.
Shoulda is maintained by Tammer Saleh, and is funded by Thoughtbot, inc.
Shoulda is Copyright © 2006-2008 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.