← back to articles

Rails Integration Test assert redirected_to :back



Published on Sep 2, 2016 - Last updated on Dec 10, 2016

#rails#ruby#programming

Given a controller action that performs a redirect_to :back, and a test that inherits from ActionDispatch::IntegrationTest and tries to assert_redirected_to previous url, we can do the following:

test 'put update object' do
    put update_object_path(object), {}, { 'HTTP_REFERER' => 'path_to_be_redirected_to' }
    assert_redirected_to path_to_be_redirected_to
end

Then we must only adjust path_to_be_redirected_to to that previous url we want.