Rails controller specs: matching on redirect URLs

Yet another thing I figured out painstakingly, by finding random bits of documentation in places other than the documentation, because apparently that’s how Rails rolls.

Here’s my situation. I have a controller spec like this:

  describe "GET checkout" do
    it "redirects to Paypal" do
      member = FactoryGirl.create(:member)
      sign_in member
      order = Order.create!(:member_id => member.id)
      get :checkout, {:id => order.to_param}
      response.should redirect_to "http://paypal.com/..."

Basically I want to check that when someone checks out, they get sent to Paypal. Easy peasy, right? The thing is, I don’t know the exact URL they’ll get sent to — it’ll be big and complicated and have lots of parameters on it, and it’ll be different in test anyway, and umm… ok, well, basically I just want to test whether I get redirected, and whether it’s to somewhere paypal-like. Ideally I want to do something like:

# doesn't work
response.should redirect_to /paypal\.com/

… and have it do a match against the URL it’s redirecting to. But that doesn’t work.

I googled every-fucking-where trying to find out what else I could do with the response object, but it doesn’t appear to be documented anywhere official. In the end I found this blog post on Response object methods by Jeff Roush. Thanks, Jeff!

In conclusion, here’s how I’m testing for a redirect that matches against a regexp:

response.status.should eq 302
response.redirect_url.should match /paypal\.com/

(There’s also a redirect_url_match? but it seems to be deprecated.)

Hope that helps someone else, and that they find the answer more quickly than I did.