使用Capybara + RSpec进行测试页面重定向

11

我有一个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'

有没有办法让匹配器等待一段时间,以便更新网址?

1个回答

27

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

expect(page).to have_current_path(Urls[target], url: true)

have_current_path 匹配器使用了Capybara的等待和重试机制,因此它会等待页面刷新。我添加了 url: true选项使其比较完整的URL。如果Urls[target]只解析为路径,那么您可以删除该选项。


谢谢!我知道一定有使用等待行为的东西。 - Alex Haynes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接