Module Shoulda::ActionController::Matchers
In: lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
lib/shoulda/action_controller/matchers/respond_with_matcher.rb
lib/shoulda/action_controller/matchers/assign_to_matcher.rb
lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb
lib/shoulda/action_controller/matchers/route_matcher.rb
lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb
lib/shoulda/action_controller/matchers/set_session_matcher.rb
lib/shoulda/action_controller/matchers/filter_param_matcher.rb
lib/shoulda/action_controller/matchers.rb
ValidationMatcher ValidatePresenceOfMatcher ValidateAcceptanceOfMatcher EnsureLengthOfMatcher ValidateFormatOfMatcher EnsureInclusionOfMatcher ValidateUniquenessOfMatcher ValidateNumericalityOfMatcher Context SetSessionMatcher RespondWithContentTypeMatcher SetTheFlashMatcher RenderWithLayout RespondWithMatcher AssignToMatcher FilterParamMatcher RouteMatcher HaveNamedScopeMatcher HaveDbIndexMatcher HaveDbColumnMatcher AllowMassAssignmentOfMatcher AllowValueMatcher AssociationMatcher HaveReadonlyAttributeMatcher lib/shoulda/context.rb lib/shoulda/action_controller/matchers/set_session_matcher.rb lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb lib/shoulda/action_controller/matchers/respond_with_matcher.rb lib/shoulda/action_controller/matchers/assign_to_matcher.rb lib/shoulda/action_controller/matchers/filter_param_matcher.rb lib/shoulda/action_controller/matchers/route_matcher.rb Matchers Macros ActionController lib/shoulda/active_record/matchers/validation_matcher.rb lib/shoulda/active_record/matchers/have_named_scope_matcher.rb lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb lib/shoulda/active_record/matchers/have_db_index_matcher.rb lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb lib/shoulda/active_record/matchers/validate_format_of_matcher.rb lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb lib/shoulda/active_record/matchers/have_db_column_matcher.rb lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb lib/shoulda/active_record/matchers/allow_value_matcher.rb lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb lib/shoulda/active_record/matchers/association_matcher.rb lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb Matchers Helpers Assertions Macros ActiveRecord ClassMethods Macros ActionView Private Helpers Assertions ActionMailer InstanceMethods Assertions Macros Shoulda dot/m_47_0.png

By using the macro helpers you can quickly and easily create concise and easy to read test suites.

This code segment:

  describe UsersController, "on GET to show with a valid id" do
    before(:each) do
      get :show, :id => User.first.to_param
    end

    it { should assign_to(:user) }
    it { should respond_with(:success) }
    it { should render_template(:show) }
    it { should not_set_the_flash) }

    it "should do something else really cool" do
      assigns[:user].id.should == 1
    end
  end

Would produce 5 tests for the show action

Methods

Public Instance methods

Ensures that the controller assigned to the named instance variable.

Options:

  • with_kind_of - The expected class of the instance variable being checked.
  • with - The value that should be assigned.

Example:

  it { should assign_to(:user) }
  it { should_not assign_to(:user) }
  it { should assign_to(:user).with_kind_of(User) }
  it { should assign_to(:user).with(@user) }

[Source]

    # File lib/shoulda/action_controller/matchers/assign_to_matcher.rb, line 18
18:       def assign_to(variable)
19:         AssignToMatcher.new(variable)
20:       end

Ensures that filter_parameter_logging is set for the specified key.

Example:

  it { should filter_param(:password) }

[Source]

    # File lib/shoulda/action_controller/matchers/filter_param_matcher.rb, line 10
10:       def filter_param(key)
11:         FilterParamMatcher.new(key)
12:       end

Ensures that the controller rendered with the given layout.

Example:

  it { should render_with_layout }
  it { should render_with_layout(:special) }
  it { should_not render_with_layout }

[Source]

    # File lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb, line 12
12:       def render_with_layout(layout = nil)
13:         RenderWithLayout.new(layout)
14:       end

Ensures a controller responded with expected ‘response’ status code.

You can pass an explicit status number like 200, 301, 404, 500 or its symbolic equivalent :success, :redirect, :missing, :error. See ActionController::StatusCodes for a full list.

Example:

  it { should respond_with(:success)  }
  it { should respond_with(:redirect) }
  it { should respond_with(:missing)  }
  it { should respond_with(:error)    }
  it { should respond_with(501)       }

[Source]

    # File lib/shoulda/action_controller/matchers/respond_with_matcher.rb, line 18
18:       def respond_with(status)
19:         RespondWithMatcher.new(status)
20:       end

Ensures a controller responded with expected ‘response’ content type.

You can pass an explicit content type such as ‘application/rss+xml’ or its symbolic equivalent :rss or a regular expression such as /rss/

Example:

  it { should respond_with_content_type(:xml)  }
  it { should respond_with_content_type(:csv)  }
  it { should respond_with_content_type(:atom) }
  it { should respond_with_content_type(:yaml) }
  it { should respond_with_content_type(:text) }
  it { should respond_with_content_type('application/rss+xml')  }
  it { should respond_with_content_type(/json/) }

[Source]

    # File lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb, line 20
20:       def respond_with_content_type(content_type)
21:         RespondWithContentTypeMatcher.new(content_type)
22:       end

Ensures that requesting path using method routes to options.

If you don‘t specify a controller, it will use the controller from the example group.

to_param is called on the options given.

Examples:

  it { should route(:get, "/posts").
                to(:controller => :posts, :action => :index) }
  it { should route(:get, "/posts/new").to(:action => :new) }
  it { should route(:post, "/posts").to(:action => :create) }
  it { should route(:get, "/posts/1").to(:action => :show, :id => 1) }
  it { should route(:edit, "/posts/1").to(:action => :show, :id => 1) }
  it { should route(:put, "/posts/1").to(:action => :update, :id => 1) }
  it { should route(:delete, "/posts/1").
                to(:action => :destroy, :id => 1) }
  it { should route(:get, "/users/1/posts/1").
                to(:action => :show, :id => 1, :user_id => 1) }

[Source]

    # File lib/shoulda/action_controller/matchers/route_matcher.rb, line 25
25:       def route(method, path)
26:         RouteMatcher.new(method, path, self)
27:       end

Ensures that a session key was set to the expected value.

Example:

  it { should set_session(:message) }
  it { should set_session(:user_id).to(@user.id) }
  it { should_not set_session(:user_id) }

[Source]

    # File lib/shoulda/action_controller/matchers/set_session_matcher.rb, line 12
12:       def set_session(key)
13:         SetSessionMatcher.new(key)
14:       end

Ensures that the flash contains the given value. Can be a String, a Regexp, or nil (indicating that the flash should not be set).

Example:

  it { should set_the_flash }
  it { should set_the_flash.to("Thank you for placing this order.") }
  it { should set_the_flash.to(/created/i) }
  it { should_not set_the_flash }

[Source]

    # File lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb, line 14
14:       def set_the_flash
15:         SetTheFlashMatcher.new
16:       end

[Validate]