How to test a javascript redirect with RSpec?

2019-07-08 21:29发布

I am using a xhr :post to interact with a controller, and I am expecting a redirect. In the js.erb i have window.location.href = address . Testing it manually, the browser gets redirected properly. How can i test it using RSpec? response.should redirect_to fails

2条回答
Summer. ? 凉城
2楼-- · 2019-07-08 21:35

I solved this particular problem inspecting the response body generated by the js.erb

response.body.should include("window.location.href")

Because it didn't feel right, I will accept my own asnwer as long as it gets upvoted.

查看更多
Luminary・发光体
3楼-- · 2019-07-08 21:44

This won't work with RSpec -- it does not execute Javascript. You'll need another testing tool -- some good suggestions in this blog post.

查看更多
登录 后发表回答