Cucumber + Capybara:在我的应用程序之外重定向浏览器的方案存在问题

时间:2010-02-19 15:41:53

标签: ruby-on-rails ruby cucumber bdd

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)

所以capybara被正确地重定向到“http://some.other.domain.com/some_path”但由于某种原因它认为它应该处理我的应用程序内的url的路径部分。 注意 capybara对“http://some.other.domain.com/”没有任何问题 - 如果我重定向到没有路径部分的网址,我的测试就会通过。

这是一个错误吗?

7 个答案:

答案 0 :(得分:6)

我想我遇到了和你一样的问题:我只是想确认一下,我的代码会使用正确的状态代码重定向到给定的URL,但我不想对该URL做任何事情。

问题是,网站按预期返回重定向,但Rack :: Test会将所有内容解释为正在测试的应用程序,并且该URL可能不存在。但是我们可以捕获错误并查看响应的样子。除了capybara的默认驱动程序,这可能不适用于其他任何东西。

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')

答案 1 :(得分:3)

以下是我写的关于使用capybara-mechanize和VCR测试外部重定向的示例。

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

答案 2 :(得分:2)

我为Capybara找到了一个很酷的解决方案(可以改编成Cucumber)。

begin
  click_button 'Accept'
rescue ActionController::RoutingError
  # Capybara doesn't redirect externally, so matches '/cb' but that route doesn't exist
  expect(page.current_url).to eq "https://example.com/cb?param=123"
end

答案 3 :(得分:1)

您使用的是哪个驱动程序? Rack-Test驱动程序不允许您从其他域请求内容。如果Capybara用Selenium或Culerity做这个,那肯定是一个bug。如果您想帮助修复它,那么编写失败的规范将非常赞赏:)

答案 4 :(得分:1)

@javascript是一个当前正在运行的解决方案,虽然还有一个mechanize driver正在使用中,它使用机架测试直到您点击外部请求。

这有点新,我还没有尝试过,但我的意思是将我的外部@javascript测试更改为使用它(用@ live或@external或类似标记)来提升速度。

答案 5 :(得分:1)

使用这个小片段:

external_redirect "https://api.twitter.com/oauth/authenticate?x_auth_access_type=read&oauth_token=TOKEN" do
  click_link "Continue with Twitter"
end

def external_redirect(url)
  yield
rescue ActionController::RoutingError # goes to twitter.com/oauth/authenticate
  current_url.must_equal url
else
  raise "Missing external redirect"
end

答案 6 :(得分:0)

我有类似的情况,我正在将我的应用程序与公司的SSO平台集成。我解决这个问题的方法是通过在场景中添加@javascript标记来使网站运行Selenium。