Simple one-liner tests for common Rails functionality
Go to file
Joe Ferris 882349634c Fixed the route builder 2009-02-07 17:45:28 -05:00
bin convert_to_shoulda_syntax: use Dir::tmpdir instead of hardcoded /tmp 2008-07-30 08:25:25 -04:00
lib Converted should_return_from_session to use a matcher 2009-02-07 17:45:28 -05:00
rails Fixed the Rails initializer to integrate with RSpec when needed 2009-02-04 19:41:08 -05:00
tasks Fix for new shoulda/tasks organization when shoulda is used solely as a plugin [#75] 2008-09-15 09:42:24 -04:00
test Fixed the route builder 2009-02-07 17:45:28 -05:00
.autotest fixed foreign key determination (http://tammer.lighthouseapp.com/projects/5807/tickets/8) 2008-01-21 14:30:10 +00:00
.gitignore Added gem related rake tasks [#55] 2008-09-14 11:12:39 -04:00
CONTRIBUTION_GUIDELINES.rdoc Added contribution guidelines 2008-06-17 13:03:25 -04:00
MIT-LICENSE converted to the MIT license 2007-11-26 15:18:29 +00:00
README.rdoc deprecate should_require_attributes with warning message. replace with should_validate_presence_of. 2009-01-29 20:45:13 -05:00
Rakefile Fixed failing matcher tests 2009-01-27 18:01:30 -05:00
controller_progress.txt Converted should_return_from_session to use a matcher 2009-02-07 17:45:28 -05:00
init.rb use the rails standard way of initializing a gem, instead of that RAILS_ROOT check 2008-10-01 17:44:09 -04:00
shoulda.gemspec Bumped to version 2.9.1 2009-02-04 19:49:30 -05:00

README.rdoc

= Shoulda - Making tests easy on the fingers and eyes

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.

= Usage

=== Context Helpers (Shoulda::Context)

Stop killing your fingers with all of those underscores...  Name your tests with plain sentences!

  class UserTest < Test::Unit::TestCase
    context "A User instance" do
      setup do
        @user = User.find(:first)
      end

      should "return its full name" do
        assert_equal 'John Doe', @user.full_name
      end

      context "with a profile" do
        setup do
          @user.profile = Profile.find(:first)
        end

        should "return true when sent #has_profile?" do
          assert @user.has_profile?
        end
      end
    end
  end

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!

=== ActiveRecord Tests (Shoulda::ActiveRecord::Macros)

Quick macro tests for your ActiveRecord associations and validations:

  class PostTest < Test::Unit::TestCase
    fixtures :all

    should_belong_to :user
    should_have_many :tags, :through => :taggings

    should_require_unique_attributes :title
    should_validate_presence_of :body, :message => /wtf/
    should_validate_presence_of :title
    should_only_allow_numeric_values_for :user_id
  end

  class UserTest < Test::Unit::TestCase
    should_have_many :posts

    should_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..100
    should_ensure_value_in_range :age, 1..100
    should_protect_attributes :password
  end

Makes TDD so much easier.

=== Controller Tests (Shoulda::Controller::Macros)

Macros to test the most common controller patterns...

  context "on GET to :show for first record" do
    setup do
      get :show, :id => 1
    end

    should_assign_to :user
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash

    should "do something else really cool" do
      assert_equal 1, assigns(:user).id
    end
  end

=== Helpful Assertions (Shoulda::Assertions)

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')

=== 3rd Party and Application Specific Macros

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.rb
  class Test::Unit::TestCase
    def self.should_be_denied(opts = {})
      should_set_the_flash_to(opts[:flash] || /Please log in/i)
      should_redirect_to(opts[:redirect]   || 'login_url')
    end
  end

= Rails Installation

=== As a Gem

Use this if you prefer to use versioned releases of shoulda.  Specify the gem dependency in your config/environment.rb file:

  Rails::Initializer.run do |config|
    config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"
  end

Then:

  $ rake gems:install
  $ rake gems:unpack

=== As a Plugin

Use this if you prefer to use the edge version of shoulda:

  $ script/plugin install git://github.com/thoughtbot/shoulda.git

=== As a Plugin (using git submodules)

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

= Credits

Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.

= License

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.