RSpec - RJS redirect (page.redirect_to)

Recently I wanted to use RSpec to test if a javascript request to my controller was being redirect to the correct place. I was using page.redirect like so:
render :update do |page|
page.redirect_to edit_my_account_cropping_url(resource.id)
end
I could not just use the standard response.should redirect to() because the response was actually rendering html that was outputting a javascript redirect.
In the end it turned out that it was quite simple. Here is the code:
it "should redirect to cropping controller" do
do_put # (not shown)
response.body.should =~ /#{my_account_cropping_url(@traveller)}/
end
Basically the do_put is a method call that I have defined which issues a put request. The response has a body element that issues a javascript redirect. We then check to see if the expected url is included in the body. If it is we can assume that the redirect is working.
Let me know if there is a better way of doing this. I don’t have any issues with the above implementation though.
Picture taken from getdown
Disqus
Comments