rspec - 使用 Capybara + RSpec 测试页面重定向

标签 rspec capybara

我有一个 step_definition

Then(/^I should be redirected to the (.+?) page/) do |target|
  expect(current_url).to eq(Urls[target])
end

并且通常效果很好。有时,当我使用 poltergeist 驱动程序时,它比正常情况下运行得更快,并且 current_url 仍然是旧页面。那是我收到这样的错误的时候:
Then I should be redirected to the login page                                                # features/step_definitions/navigate-steps.rb:64

expected: "http://example.com/"
got: "http://example.com/reset-password"

(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/navigation.rb:50:in `/^I should be redirected to the (.+?) page$/'
features/password.feature:100:in `Then I should be redirected to the login page'

有没有办法让匹配器等待 url 更新?

最佳答案

不要使用 eq匹配器 current_pathcurrent_url .相反,使用 have_current_path Capybara 2.5+ 提供的匹配器

expect(page).to have_current_path(Urls[target], url: true)
have_current_path matcher 使用 Capybara 的等待/重试行为,因此它将等待页面更改。我添加了 url: true选项使其比较完整的网址。如 Urls[target]解析为只是一个路径,您可以删除该选项。

关于rspec - 使用 Capybara + RSpec 测试页面重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533512/

相关文章:

selenium-webdriver - 如何等待并接受带有 capybara / Selenium 的警报框

ruby-on-rails - Capybara 使用什么库作为 CSS 选择器?

ruby-on-rails - rails 。 Capybara fill_in 找到文本输入但没有 fill_in

ruby - lambda 和 begin block 有什么区别?

ruby-on-rails - 在验收规范中测试或不测试什么?

ruby - 如何使用 RSpec 模拟/ stub 文件目录及其内容?

ruby-on-rails - capybara 和 Rspec。 capybara 不在测试中呈现页面,在 Rails 中使用访问或获取方法

ruby - 在 Windows 操作系统中使用 Headless 的 Capybara

testing - Rspec 测试布局

ruby - Capybara 的 have_selector 有哪些选项?