2015-11-04 157 views
6

测试页面重定向我有一个step_definition与水豚+ RSpec的

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更新?

回答

13

请勿使用eq匹配器与current_pathcurrent_url。相反,使用由水豚2.5+

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

提供的have_current_path匹配的have_current_path匹配使用水豚的等待/重试的行为,所以它会等待页面更改。我添加了url: true选项,以便比较完整的网址。如果Urls[target]解析为仅路径,您可以删除该选项。

+0

谢谢!我知道必须有一些使用等待行为的东西。 –