lunes, 28 de septiembre de 2009

Testing con "redirect_to :back"

Si necesitas testear un controlador rails que contiene un "redirect_to :back" obtendrás un error al lanzar el test. Si vamos a la implementación, podemos encontrar:

  • :back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])
Si estamos testeando no tendremos un request, por lo que fallará la redirección y por lo tanto el test.

SOLUCIÓN:

def test_should_get_new
   @request.env['HTTP_REFERER'] = 'http://whatever'
   get :new, :link_id => 1
   assert_redirected_to 'http://whatever'
end