Ruby on rails rspec/水豚<;李>;未加载

Ruby on rails rspec/水豚<;李>;未加载,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我正试图用Rspec/Capybara测试我的AJAX 我的页面(招聘人员仪表板)包含3列 它加载申请某个空缺的候选人及其状态 第1列=>状态==挂起 第2列=>状态==匹配 第3列==>状态==“已密封” 在我的规范中,我正在创建一个空缺,其中有一个申请者的状态待定 print "Amount of vacancies => #{Vacancy.count} " print "Amount of candidates => #{Employee.count} " print "Am

我正试图用Rspec/Capybara测试我的AJAX

我的页面(招聘人员仪表板)包含3列

它加载申请某个空缺的候选人及其状态

第1列=>状态==挂起

第2列=>状态==匹配

第3列==>状态==“已密封”

在我的规范中,我正在创建一个空缺,其中有一个申请者的状态待定

print "Amount of vacancies => #{Vacancy.count} "
print "Amount of candidates => #{Employee.count} "
print "Amount of candidates applied for vacancy => #{Matching.where(vacancy_id: Vacancy.first.id).count}"
print "State of #{Employee.first.name} for #{Vacancy.first.name} => #{Matching.where(vacancy_id: Vacancy.first.id, employee_id: Employee.first.id).first.state}"
返回

Amount of vacancies => 1
Amount of candidates => 1
Amount of candidates applied for vacancy => 1
State of foo1 Hintz for vacancy1 => pending
expected: 1
got: 0
因此,这意味着该候选者应加载到以下li中:

<ul id="applied-desktop-dashboard-ajax">
  <li>
    CANDIDATES
  </li>
</ul>
返回

Amount of vacancies => 1
Amount of candidates => 1
Amount of candidates applied for vacancy => 1
State of foo1 Hintz for vacancy1 => pending
expected: 1
got: 0
当我

 save_and_open_page
我看到李是空的

所以我试过了

sleep(5)
之后

但是没有成功

有人知道为什么我的li没有在这个测试中加载(但是在本地主机上工作很好)吗

全面测试:

require 'rails_helper'

RSpec.feature "Creating vacancies" do

  before do
    create(:matching)
  end

  scenario "Ajax testing" do
    visit "/recruiters/sign_in"
    fill_in "Email", with: "bedrijf1@hotmail.be"
    fill_in "Password", with: "bedrijf1bedrijf1"

    within 'form#new_recruiter' do
      find('input[name="commit"]').click
    end

    expect(current_path).to eq '/'

    visit "/dashboard"

    sleep(5)



    print "Amount of vacancies => #{Vacancy.count} "
    print "Amount of candidates => #{Employee.count} "
    print "Amount of candidates applied for vacancy => #{Matching.where(vacancy_id: Vacancy.first.id).count}"
    print "State of #{Employee.first.name} for #{Vacancy.first.name} => #{Matching.where(vacancy_id: Vacancy.first.id, employee_id: Employee.first.id).first.state}"

    # save_and_open_page
    page.all("#applied-desktop-dashboard-ajax li").count.should eql(1)

  end
end

当执行js测试时,被测试的应用程序在与测试不同的线程中运行,这意味着它们不再共享相同的数据库连接-请参阅。因此,您需要禁用事务测试,并使用截断或删除来管理数据库状态。最简单的方法是使用database_cleaner并设置一个配置,为每个测试切换到所需的策略-

一旦您正确地配置了它,那么是时候检查一下您的测试了,并通过异步测试使它不那么脆弱

  • expect(当前路径)。到eq'/'
    应重写为
    expect(第页)。到具有当前路径('/')
    ,以便水豚将自动等待页面更改
  • 默认情况下,page.all不会等待任何元素出现,因为0个元素是有效的返回值。如果您更改
    page.all(“#applied desktop dashboard ajax li”).count.eql(1)
    应更改为
    expect(page)。要使用_选择器(“#applied desktop dashboard ajax li”,count:1)
    Capybara将等待一段时间,让li显示在页面上,而不是因为ajax尚未完成而立即失败。您还可以指定:minimum、:max、:between,具体取决于您要验证的内容

  • 使用正确的Capybara方法可以消除测试中对大多数(如果不是全部的话)sleep()的需要。

    您使用哪种驱动程序?您可能需要将
    js:true
    传递到您的场景中才能使用javascript驱动程序。我添加了js:true,这促使我安装selenium并安装firefox,我做到了。现在它运行,但在登录屏幕上停止,无法登录。它使用的是一开始创建的同一个用户吗?当我输入“#{Recruiter.first.email}”时,我仍然可以看到用户,但是当我登录时,它不起作用。