使用assert_redirected_to测试状态代码

时间:2013-01-04 22:08:11

标签: ruby-on-rails

在“编辑”控制器方法上,如果找不到作为参数传递的项目,我想将用户重定向到控制器索引操作,并带有:not_found状态代码。这是代码:

redirect_to dashboard_posts_url, :status => :not_found

问题在于我无法使用assert_redirected_to rails断言测试此行为,因为它期望状态代码为:redirect,如源代码中所示:http://www.rubydoc.info/docs/rails/ActionDispatch/Assertions/ResponseAssertions:assert_redirected_to :(

你有没有重写断言的解决方案?

1 个答案:

答案 0 :(得分:1)

您可以先测试响应类型,

assert_response :not_found

然后,将响应类型更改为:重定向,以便assert_redirected_to不会失败,

response.status = :redirect

最后,测试重定向路径

assert_redirected_to(...)