在进行 Rails 集成故事测试时如何处理 AJAX 重定向?
我试图弄清楚如何运行基于故事的集成测试以及 AJAX 重定向在“故事”期间出现的位置。
简单的登录示例:我使用登录到网站,
def user.logs_in(email, pwd)
get root_path
assert_response :success
assert_template 'index'
post :post, session_path, :email => email, :pwd => pwd
assert_response :redirect
assert_redirect_to backend_path
follow_redirect!
assert_response :success
assert_template 'index'
assert session[:user_id]
end
不幸的是,登录过程是基于 AJAX 的,并安装了它返回的 301 重定向,
document.location.href = "<%= backend_path >";
这会返回 200 代码,意味着
assert_response :redirect
失败。
如何在集成测试中处理 AJAX 重定向?
I'm trying to figure out how to run integration tests that are storybased and where AJAX redirects appear during the 'story'.
Simple login example: I login to the website using
def user.logs_in(email, pwd)
get root_path
assert_response :success
assert_template 'index'
post :post, session_path, :email => email, :pwd => pwd
assert_response :redirect
assert_redirect_to backend_path
follow_redirect!
assert_response :success
assert_template 'index'
assert session[:user_id]
end
Unfortunately the login process is AJAX based and insted of a 301-redirect it returns
document.location.href = "<%= backend_path >";
which gives back a 200-code meaning that
assert_response :redirect
fails.
How can i handle AJAX redirects in my integration tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有重定向,当然您将无法(成功)测试它。因此,请测试您正在执行的操作:您希望响应为
200
,正文为 Javascript 片段。或者也可以使用assert_template
。如果你想测试你的 Javascript-Login-Button 是否真的评估了 js 主体以及浏览器对此有何反应,你必须使用像 Selenium 这样的东西。
If there is no redirect, of course you won't be able to (successfully) test for it. So instead test for what you are doing: You want the response to be a
200
and the body to be a Javascript snippet. Or just useassert_template
, too.If you want to test if your Javascript-Login-Button actually evals the js body and how the browser reacts to that you would have to use something like Selenium.