草庐IT

ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app

coder 2025-05-13 原文

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/”没有任何问题——如果我重定向到一个没有路径部分的 url,我的测试通过。

这是一个错误吗?

最佳答案

我想我遇到了和您一样的问题:我只是想确认,我的代码重定向到具有正确状态代码的给定 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')

关于ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2297497/

有关ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app的更多相关文章

随机推荐