2015-11-04 203 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

謝謝!我知道必須有一些使用等待行爲的東西。 –