【问题标题】:Rspec feature test: Cannot visit a pathRspec功能测试:无法访问路径
【发布时间】:2017-12-15 15:45:31
【问题描述】:

我的 rspec 功能测试都失败了,因为我无法访问指定的路径。登录后他们似乎都卡在了根路径上。屏幕截图显示该页面仍然保留在根路径上。测试步骤在浏览器上运行,这意味着路由是正确的。有什么想法吗?

我收到以下测试错误消息:

失败/错误:page.evaluate_script('jQuery.active').zero?

我的功能规范测试摘录:

describe 'follow users' do

    let!(:user) { FactoryGirl.create(:user) }
    let!(:other_user) { FactoryGirl.create(:friend) }

  describe "Managing received friend request", js: true do

    let!(:request) { Friendship.create(user_id: other_user.id, friend_id: user.id, accepted: false) }

    before do
       login_as(user, :scope => :user)
       visit followers_path
    end

    it 'friend request disappear once user clicks accept' do
      click_on "Accept"
      wait_for_ajax
      expect(current_path).to eq(followers_path)
      expect(page).to have_css(".pending-requests", text: "You have 0 pending friend requests")
      expect(page).to_not have_css(".pending-requests", text: other_user.name)
      expect(page).to_not have_link("Accept")
      expect(page).to_not have_link("Decline")
  end

  end
end

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    这里的问题是您在不包含 jQuery 的页面上或尚未加载的时候调用“wait_for_ajax”。解决方案是停止使用wait_for_ajax,而是按照设计使用 Capybara 期望/匹配器。在极少数情况下实际上需要wait_for_ajax,即便如此,这通常也是用户界面决策错误的迹象(没有向用户表明正在发生某些事情)。您也不应该将eq 匹配器与current_path 一起使用,而应该使用Capybara 提供的have_current_path 匹配器,因为它具有与Capybara 提供的所有匹配器一样的等待/重试行为。

    it 'friend request disappear once user clicks accept' do
      click_on "Accept"
      expect(page).to have_current_path(followers_path)
      expect(page).to have_css(".pending-requests", text: "You have 0 pending friend requests")
      expect(page).to_not have_css(".pending-requests", text: other_user.name)
      expect(page).to_not have_link("Accept")
      expect(page).to_not have_link("Decline")
    end
    

    如果这对您不起作用,那么按钮单击实际上并没有触发页面更改(检查您的测试日志),您的Capybara.default_max_wait_time 设置不够高,无法满足您正在测试的硬件,您的 login_as 语句实际上并未登录用户(尽管我希望单击接受按钮会失败),或者您的应用程序中有错误。

    如果login_as 没有实际登录,那么请确保用于运行 AUT 的服务器与测试在同一进程中运行,如果您使用的是 puma,则意味着确保在输出它并没有说它在集群模式下运行。

    【讨论】:

    • 感谢@ThomasWalpole,我已删除 wait_for_ajax 并更改为 have_current_path,但我的测试仍然失败。我有许多其他测试失败并在 ajax 调用后表现出奇怪的行为,要么没有执行 click_on 要么卡在 root_path (当我不久前测试它们并且它们都通过时,它们现在都失败了)。我知道应该使用 expect(page).to have_content 而不是 wait_for_ajax 进行 ajax 调用。麻烦您看看我的 github 存储库中的 rspec 测试:github.com/dianameow/diana-portal/tree/master/spec
    • 我想我已经将问题缩小到水豚的 click_on 在我所有的测试中都不起作用
    • @doyz 我试过运行你的测试,其中很多都有不同的错误,比如关于 nil 上缺少方法的错误等。但是只运行您单独询问的一项测试,它对我来说很好,不确定您在做什么,我建议删除并重新安装 capybara-webkit gem
    • @doyz 另外你可能想更新 Capybara - 2.7.1 在这一点上已经很古老了。
    • @doyz 我去尝试了您的最新提交 - 由于页面中的 JS 错误而失败。具体来说,您的 pages.js 在某处缺少})。在测试模式下,当所有 JS 连接在一起时,单个文件中的错误可能会阻止文件的其余部分被处理。在开发模式下,一个文件中的错误只会停止处理该文件。在这里它阻止了rails ujs的东西被加载,所以当你点击“关注”按钮时,它提交的是一个get而不是一个帖子,然后你的应用程序会捕获404并重定向到/(也许不要在dev中捕获404s /测试模式)
    【解决方案2】:

    尝试使用这种方法等待所有 ajax 请求完成:

    def wait_for_ajax
      Timeout.timeout(Capybara.default_wait_time) do
        active = page.evaluate_script('jQuery.active')
        until active == 0
          active = page.evaluate_script('jQuery.active')
        end
      end
    end
    

    取自:Wait for ajax with capybara 2.0

    【讨论】:

    • 您好,感谢您的建议,但是使用 wait_for_ajax 助手会导致错误消息 Failure/Error: page.evaluate_script('jQuery.active').zero?我尝试改用 expect(page).to have_content(),但屏幕截图仍然显示根路径页面
    • 你用的是什么版本的水豚?
    • 我使用的是 2.7.1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多