【问题标题】:Capybara doesn't follow redirectCapybara 不遵循重定向
【发布时间】:2015-11-11 09:35:59
【问题描述】:

我试图在 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 上的期望通过?
  • 为什么点击链接到一个 url 不会导致 current_url 的期望值通过?

【问题讨论】:

  • 1) 如果重定向需要时间,请使用sleep 2) it{ should have_a_uri_of URI.parse(current_url) } 3) 您已经通过此its(:current_url){ should have_a_uri_of root_path }current_url 分配给root_path,这就是为什么每次都需要root_path真正的当前网址
  • 请修正缩进

标签: ruby-on-rails rspec capybara


【解决方案1】:

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

it { should have_current_path(root_path) }

expect(page).to have_current_path(root_path)

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2019-05-10
    • 2020-10-04
    • 2017-04-09
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多