public
Description: Makes tests easy on the fingers and the eyes
Home | Edit | New

ActionController Helpers

Just like the ActiveRecord macros, Shoulda gives you a set of macros to make testing your controllers as clear as possible. All of these methods are in the Shoulda RDocs, but here’s a quick example:

class UsersControllerTest < Test::Unit::TestCase
  context "on GET to :show" do
    setup { get :show, :id => 1 }

    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

  context "on POST to :create" do
    setup { post :create, :user => {:name => 'Ronald', :party => 'Repukeulan' } }
    should_assign_to :user
    should_redirect_to("the new users page") { user_url(assigns :user) }
    should_set_the_flash_to(/created/i)
  end
end
Last edited by adamhunter, Thu Jul 30 07:53:38 -0700 2009
Home | Edit | New
Versions: