Class: Merb::Test::Rspec::ControllerMatchers::RedirectTo

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-core/test/matchers/controller_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, options = {}) ⇒ RedirectTo

Parameters

String

The expected location

Hash

Optional hash of options (currently only :message)



39
40
41
42
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 39

def initialize(expected, options = {})
  @expected = expected
  @options  = options
end

Instance Method Details

#failure_messageObject

Returns

String

The failure message.



64
65
66
67
68
69
70
71
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 64

def failure_message
  msg = "expected #{inspect_target} to redirect to <#{@expected}>, but "
  if @redirected
    msg << "was <#{target_location}>"
  else
    msg << "there was no redirection"
  end
end

#inspect_targetObject

Returns

String

The controller and action name.



81
82
83
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 81

def inspect_target
  "#{@target.controller_name}##{@target.action_name}"
end

#matches?(target) ⇒ Boolean

Parameters

target<Merb::Controller>

The controller to match

Returns

Boolean

True if the controller status is redirect and the locations match.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 50

def matches?(target)
  @target, @location = target, target.headers['Location']
  @redirected = BeRedirect.new.matches?(target.status)

  if @options[:message]
    msg = Merb::Request.escape([Marshal.dump(@options[:message])].pack("m"))
    @expected << "?_message=#{msg}"
  end
  
  @location == @expected && @redirected
end

#negative_failure_messageObject

Returns

String

The failure message to be displayed in negative matches.



75
76
77
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 75

def negative_failure_message
  "expected #{inspect_target} not to redirect to <#{@expected}>, but did anyway"
end

#target_locationObject

Returns

String

Either the target’s location header or the target itself.



87
88
89
# File 'lib/merb-core/test/matchers/controller_matchers.rb', line 87

def target_location
  @target.respond_to?(:headers) ? @target.headers['Location'] : @target
end