Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber+Capybara: Problem with a scenario that redirects the browser outside of my app

Given I have a rails app
And I'm using cucumber
And I'm using capybara
And I have an action that results in a redirect_to "http://some.other.domain.com/some_path"
When I test this action
Then the in-app portion of the test works fine
But I see this error: No route matches "/some_path" with {:method=>:get} (ActionController::RoutingError)

So capybara is properly redirected to "http://some.other.domain.com/some_path" but for some reason it thinks it should handle the path portion of the url inside my app. NOTE capybara has no problem at all with "http://some.other.domain.com/" -- my tests pass if I redirect to a url without a path portion.

Is this a bug?

like image 552
joshsz Avatar asked Feb 19 '10 15:02

joshsz


2 Answers

I think I had the same problem as you: I just wanted to confirm, that my code redirects to that given URL with the correct status code, but I don't want to do anything on that URL.

The problem is, that the site returns the redirect as expected, but Rack::Test interprets everything to the application under test, and that URL probably doesn't exist. But we can just catch the error and see what the response looked like. This will probably not work with anything else than capybara's default driver.

begin
  click_button('Pay with Paypal')
rescue ActionController::RoutingError
end

expect(page.status_code).to eq(302)
expect(page.response_headers['Location']).to include('paypal.com/cgi-bin/websrc')
like image 176
iGEL Avatar answered Oct 19 '22 11:10

iGEL


Here's an example I wrote up about using capybara-mechanize and VCR to test an external redirect.

http://blog.tddium.com/2011/10/04/testing-external-redirects-vcr-capybara-mechanize/

like image 3
Jay Moorthi Avatar answered Oct 19 '22 10:10

Jay Moorthi