thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb

102 lines
2.7 KiB
Ruby
Raw Normal View History

module Shoulda
2010-12-15 22:34:19 +00:00
module Matchers
module ActionController
# The `redirect_to` matcher tests that an action redirects to a certain
# location. In a test suite using RSpec, it is very similar to
# rspec-rails's `redirect_to` matcher. In a test suite using Test::Unit /
# Shoulda, it provides a more expressive syntax over
# `assert_redirected_to`.
2010-12-15 22:34:19 +00:00
#
# class PostsController < ApplicationController
# def show
# redirect_to :index
# end
# end
#
# # RSpec
# describe PostsController do
# describe 'GET #show' do
# before { get :show }
#
# it { should redirect_to(posts_path) }
# it { should redirect_to(action: :index) }
# end
# end
#
# # Test::Unit
# class PostsControllerTest < ActionController::TestCase
# context 'GET #show' do
# setup { get :show }
#
# should redirect_to { posts_path }
# should redirect_to(action: :index)
# end
# end
#
# @return [RedirectToMatcher]
2010-12-15 22:34:19 +00:00
#
def redirect_to(url_or_description, &block)
RedirectToMatcher.new(url_or_description, self, &block)
end
# @private
class RedirectToMatcher
attr_reader :failure_message, :failure_message_when_negated
alias failure_message_for_should failure_message
alias failure_message_for_should_not failure_message_when_negated
2010-12-15 22:34:19 +00:00
def initialize(url_or_description, context, &block)
@url_block = nil
@url = nil
@context = context
@failure_message = nil
@failure_message_when_negated = nil
2010-12-15 22:34:19 +00:00
if block
@url_block = block
@location = url_or_description
2010-12-15 22:34:19 +00:00
else
@location = @url = url_or_description
2010-12-15 22:34:19 +00:00
end
end
def in_context(context)
@context = context
self
end
def matches?(controller)
@controller = controller
redirects_to_url?
end
def description
"redirect to #{@location}"
end
private
def redirects_to_url?
begin
2014-02-01 21:09:44 +00:00
@context.__send__(:assert_redirected_to, url)
@failure_message_when_negated = "Didn't expect to redirect to #{url}"
2010-12-15 22:34:19 +00:00
true
rescue Shoulda::Matchers::AssertionError => error
@failure_message = error.message
2010-12-15 22:34:19 +00:00
false
end
end
2012-03-30 15:22:31 +00:00
def url
if @url_block
@context.instance_eval(&@url_block)
else
@url
end
end
2010-12-15 22:34:19 +00:00
end
end
end
end