2014-09-04 67 views
1

我試圖在page.current_url上運行預期,但是,由於某些原因,page.current_url沒有得到更新。水豚不遵循重定向

發生這種情況當我點擊鏈接而不是訪問網址。看:

scenario 'When a user who is not an admin visits the dashboard' do 
    before do 
     sign_up user 
     visit admin_statistics_path 
    end 

    its(:current_url){ should have_a_uri_of root_path } 
end 

所有這些規格通過。然而,當我點擊,而不是訪問admin_statistics_path鏈接到admin_statistics_path的current_url預期失敗:

describe 'When a user who is not an admin clicks control panel' do 
     before do 
      sign_up user 
      click_link 'dashboard' 

#   puts current_url 
     end 

    it { should have_css 'div.danger', "You do not have sufficient priviledges to access the admin area. Try logging out and logging in with an account that has admin priviledges." } 
    it { should have_css 'h1', text: 'Goals'} 
    its(:current_url){ should have_a_uri_of root_path } 
end 

所有上述預期從current_url一個通過開!即使怪異,如果我把current_url,規範通:

describe 'When a user who is not an admin clicks control panel' do 
     before do 
      sign_up user 
      click_link 'dashboard' 

      puts current_url #=> returns the initial url despite the specs passing 
     end 

    it { should have_css 'div.danger', "You do not have sufficient priviledges to access the admin area. Try logging out and logging in with an account that has admin priviledges." } 
    it { should have_css 'h1', text: 'Goals'} 
    its(:current_url){ should have_a_uri_of root_path } 
end 
  • 我如何獲得水豚等待重定向?
  • 爲什麼把current_url導致預期通過?
  • 爲什麼訪問 url會導致current_url的期望值通過?
  • 爲什麼點擊一個鏈接不會導致current_url的期望值通過?
+0

1)使用'sleep'時將使用水豚等待行爲has_current_path匹配如果重定向需要時間2)'it {should_a_uri_of URI.parse(current_url)}'3)您已經通過這個'its(:current_url){should have_a_uri_of root_path}'將'current_url'賦值給'root_path''這就是爲什麼每次它採取'root_path'而不是真正的目前的網址 – 2014-09-04 10:51:23

+2

並請修復縮進 – 2015-08-19 16:27:08

回答

2

對於任何來到這個問題現在,水豚2.5添加檢查的current_path

it { should have_current_path(root_path) } 

expect(page).to have_current_path(root_path)