Module: Shoulda::ActionController::Matchers

Included in:
ActiveSupport::TestCase, RSpec::Rails::ControllerExampleGroup, Macros, Test::Unit::TestCase
Defined in:
lib/shoulda/action_controller/matchers.rb,
lib/shoulda/action_controller/matchers/route_matcher.rb,
lib/shoulda/action_controller/matchers/assign_to_matcher.rb,
lib/shoulda/action_controller/matchers/redirect_to_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/respond_with_matcher.rb,
lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb,
lib/shoulda/action_controller/matchers/render_template_matcher.rb,
lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb,
lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb

Overview

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

Defined Under Namespace

Classes: AssignToMatcher, FilterParamMatcher, RedirectToMatcher, RenderTemplateMatcher, RenderWithLayout, RespondWithContentTypeMatcher, RespondWithMatcher, RouteMatcher, SetSessionMatcher, SetTheFlashMatcher

Instance Method Summary collapse

Instance Method Details

#assign_to(variable) ⇒ Object

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


18
19
20
# File 'lib/shoulda/action_controller/matchers/assign_to_matcher.rb', line 18

def assign_to(variable)
  AssignToMatcher.new(variable)
end

#filter_param(key) ⇒ Object

Ensures that filter_parameter_logging is set for the specified key.

Example:

it { should filter_param(:password) }


10
11
12
# File 'lib/shoulda/action_controller/matchers/filter_param_matcher.rb', line 10

def filter_param(key)
  FilterParamMatcher.new(key)
end

#redirect_to(url_or_description, &block) ⇒ Object

Ensures a controller redirected to the given url.

Example:

it { should redirect_to('http://somewhere.com')  }
it { should redirect_to(users_path)  }


11
12
13
# File 'lib/shoulda/action_controller/matchers/redirect_to_matcher.rb', line 11

def redirect_to(url_or_description, &block)
  RedirectToMatcher.new(url_or_description, self, &block)
end

#render_template(template) ⇒ Object

Ensures a controller rendered the given template.

Example:

it { should render_template(:show)  }


10
11
12
# File 'lib/shoulda/action_controller/matchers/render_template_matcher.rb', line 10

def render_template(template)
  RenderTemplateMatcher.new(template, self)
end

#render_with_layout(expected_layout = nil) ⇒ Object

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 }


12
13
14
# File 'lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb', line 12

def render_with_layout(expected_layout = nil)
  RenderWithLayout.new(expected_layout)
end

#respond_with(status) ⇒ Object

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


18
19
20
# File 'lib/shoulda/action_controller/matchers/respond_with_matcher.rb', line 18

def respond_with(status)
  RespondWithMatcher.new(status)
end

#respond_with_content_type(content_type) ⇒ Object

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/) }


20
21
22
# File 'lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb', line 20

def respond_with_content_type(content_type)
  RespondWithContentTypeMatcher.new(content_type)
end

#route(method, path) ⇒ Object

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


25
26
27
# File 'lib/shoulda/action_controller/matchers/route_matcher.rb', line 25

def route(method, path)
  RouteMatcher.new(method, path, self)
end

#set_session(key) ⇒ Object

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


12
13
14
# File 'lib/shoulda/action_controller/matchers/set_session_matcher.rb', line 12

def set_session(key)
  SetSessionMatcher.new(key)
end

#set_the_flashObject

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 }


14
15
16
# File 'lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb', line 14

def set_the_flash
  SetTheFlashMatcher.new
end