Class: RiotRails::ActionController::RedirectedToMacro

Inherits:
Riot::AssertionMacro
  • Object
show all
Defined in:
lib/riot/action_controller/assertion_macros.rb

Overview

Asserts that the response from an action is a redirect and that the path or URL matches your expectations. If the response code is not in the 300s, the assertion will fail. If the reponse code is fine, but the redirect-to path or URL do not exactly match your expectation, the assertion will fail.

redirected_to expects you to provide your expected path in a lambda. This is so you can use named routes, which are - as it turns out - handy. It’s also what I would expect to be able to do.

controlling :people
setup do
  post :create, :person { ... }
end

controller.redirected_to { person_path(...) }

PS: There is a difference between saying named_route_path and named_route_url and Riot Rails will be very strict (read: annoying) about it :)

Instance Method Summary collapse

Instance Method Details

#evaluate(actual, expected_redirect) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/riot/action_controller/assertion_macros.rb', line 96

def evaluate(actual, expected_redirect)
  actual_response_code = actual.response.response_code
  if (300...400).member?(actual_response_code)
    actual_redirect = actual.url_for(actual.response.redirected_to)
    msg = "expected to redirect to <#{expected_redirect}>, not <#{actual_redirect}>"
    expected_redirect == actual_redirect ? pass("redirected to #{expected_redirect}") : fail(msg)
  else
    fail("expected response to be a redirect, but was #{actual_response_code}")
  end
end