module Shoulda::ActionController::Macros

def should_assign_to(*names, &block)

should_assign_to(:user) { @user }
should_assign_to :user, :class => User
should_assign_to :user, :posts

Example:

the return value of that block.
If a block is passed, the assigned variable is expected to be equal to

* :class - The expected class of the instance variable being checked.
Options:

each of the named instance variable(s).
Macro that creates a test asserting that the controller assigned to
def should_assign_to(*names, &block)
  klass = get_options!(names, :class)
  names.each do |name|
    matcher = assign_to(name).with_kind_of(klass)
    should matcher.description do
      if block
        expected_value = instance_eval(&block)
        matcher = matcher.with(expected_value)
      end
      assert_accepts matcher, @controller
    end
  end
end

def should_filter_params(*keys)

should_filter_params :password, :ssn

Example:

is set for the specified keys
Macro that creates a test asserting that filter_parameter_logging
def should_filter_params(*keys)
  keys.each do |key|
    matcher = filter_param(key)
    should matcher.description do
      assert_accepts matcher, @controller
    end
  end
end

def should_not_assign_to(*names)

should_not_assign_to :user, :posts

Example:

any of the named instance variable(s).
Macro that creates a test asserting that the controller did not assign to
def should_not_assign_to(*names)
  names.each do |name|
    matcher = assign_to(name)
    should "not #{matcher.description}" do
      assert_rejects matcher, @controller
    end
  end
end

def should_not_set_the_flash

Macro that creates a test asserting that the flash is empty.
def should_not_set_the_flash
  matcher = set_the_flash
  should "not #{matcher.description}" do
    assert_rejects matcher, @controller
  end
end

def should_redirect_to(description, &block)

should_redirect_to("the user's profile") { user_url(@user) }

Example:

for the redirect.
generating a test name. Expects a block that returns the expected path
redirect to the given path. The passed description will be used when
Macro that creates a test asserting that the controller returned a
def should_redirect_to(description, &block)
  should "redirect to #{description}" do
    expected_url = instance_eval(&block)
    assert_redirected_to expected_url
  end
end

def should_render_template(template)

should_render_template :new

Example:
Macro that creates a test asserting that the controller rendered the given template.
def should_render_template(template)
  should "render template #{template.inspect}" do
    assert_template template.to_s
  end
end

def should_render_with_layout(expected_layout = 'application')

should_render_with_layout 'special'

Example:
Macro that creates a test asserting that the controller rendered with the given layout.
def should_render_with_layout(expected_layout = 'application')
  matcher = render_with_layout(expected_layout)
  if expected_layout
    should matcher.description do
      assert_accepts matcher, @controller
    end
  else
    should "render without layout" do
      assert_rejects matcher, @controller
    end
  end
end

def should_render_without_layout

Same as @should_render_with_layout false@
Macro that creates a test asserting that the controller rendered without a layout.
def should_render_without_layout
  should_render_with_layout nil
end

def should_respond_with(response)

should_respond_with :success

Example:
Macro that creates a test asserting that the controller responded with a 'response' status code.
def should_respond_with(response)
  should "respond with #{response}" do
    matcher = respond_with(response)
    assert_accepts matcher, @controller
  end
end

def should_respond_with_content_type(content_type)

should_respond_with_content_type /rss/
should_respond_with_content_type :rss
should_respond_with_content_type 'application/rss+xml'

Example:
Macro that creates a test asserting that the response content type was 'content_type'.
def should_respond_with_content_type(content_type)
  matcher = respond_with_content_type(content_type)
  should matcher.description do
    assert_accepts matcher, @controller
  end
end

def should_route(method, path, options)


:action => :show, :id => 1, :user_id => 1
should_route :get, "/users/1/posts/1",
should_route :delete, "/posts/1", :action => :destroy, :id => 1
should_route :put, "/posts/1", :action => :update, :id => 1
should_route :edit, "/posts/1", :action => :show, :id => 1
should_route :get, "/posts/1", :action => :show, :id => 1
should_route :post, "/posts", :action => :create
should_route :get, "/posts/new", :action => :new
should_route :get, "/posts", :controller => :posts, :action => :index

Examples:

+to_param+ is called on the +options+ given.

based on the current test.
If you don't specify a :controller, it will try to guess the controller

given +options+.
+method+ on the given +path+, and asserts that it routes to the
Macro that creates a routing test. It tries to use the given HTTP
def should_route(method, path, options)
  unless options[:controller]
    options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
  end
  matcher = route(method, path).to(options)
  should matcher.description do
    assert_accepts matcher.in_context(self), self
  end
end

def should_set_session(key, &block)

should_set_session(:message) { "Free stuff" }
should_set_session(:user_id) { @user.id }

Example:

that returns the expected value.
session is correct. Expects the session key as a parameter, and a block
Macro that creates a test asserting that a value returned from the
def should_set_session(key, &block)
  matcher = set_session(key)
  should matcher.description do
    expected_value = instance_eval(&block)
    matcher = matcher.to(expected_value)
    assert_accepts matcher, @controller
  end
end

def should_set_the_flash_to(val)

should_set_the_flash_to /created/i
should_set_the_flash_to "Thank you for placing this order."

Example:

This behavior is deprecated.
If the argument is +nil+, it will assert that the flash is not set.

value. Expects a +String+ or +Regexp+.
Macro that creates a test asserting that the flash contains the given
def should_set_the_flash_to(val)
  if val
    matcher = set_the_flash.to(val)
    should matcher.description do
      assert_accepts matcher, @controller
    end
  else
    warn "[DEPRECATION] should_set_the_flash_to nil is deprecated. " <<
         "Use should_not_set_the_flash instead."
    should_not_set_the_flash
  end
end