Capybara不遵循重定向

时间:2014-09-04 10:16:14

标签: ruby-on-rails rspec capybara

我试图在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
  • 如何让capybara等待重定向?
  • 为什么把current_url放到预期会通过?
  • 为什么访问一个网址导致对current_url的期望通过?
  • 为什么点击链接到网址不会导致对current_url的期望通过?

1 个答案:

答案 0 :(得分:2)

对于现在遇到这个问题的人,Capybara 2.5添加了一个has_current_path匹配器,它将在检查current_path时使用Capybaras等待行为

it { should have_current_path(root_path) }

expect(page).to have_current_path(root_path)