RSpec - select枣茧gem

RSpec - select date cocoon gem

我正在对我的 rails 应用程序进行一些测试。关于某些表格,我正在使用 gem cocoon。我在测试 "date selection" 时遇到了一些麻烦。我想知道我做错了什么,非常感谢您的帮助!!

请在下面找到我到目前为止所做的事情:

工作表单的嵌套字段(我正在尝试测试输入字段 started_at)

    .nested-fields
      = f.input :company_name,  label:'Nom de entreprise', input_html: {class: "form-control job-company-name"}
      = f.input :title, label: 'Votre fonction', input_html: {class: "form-control job-title "}
      = f.input :location, label: 'Lieu de travail', input_html: {class: "form-control job-location"}
      = f.input :started_at, discard_day: true, order: [:month, :year], include_blank: true, label:'Date début',input_html: {class: "work-started-at job-started-at"}, start_year: 1970, end_year: 2016, :id => 'task_target_date'
      = f.input :finished_at, discard_day: true, order: [:month, :year], label: 'Date fin ( mettre le mois en cours si c\'est votre emploi actuel)',input_html: {class: "work-finished-at end_date job-finished-at"},start_year: 1970, end_year: 2016, :default => Time.now   
      = f.input :description, label: 'Vos responsabilités',:input_html => {:rows => 9, style: 'max-width:100%;', class: ""}
   .remove-experience.text-center
      = link_to_remove_association "supprimer cette expérience", f, class: 'btn btn-delete form-button'

RSpec 和水豚

require 'rails_helper'

RSpec.feature "Edit Profile" do

  scenario "Profile updated successfully ", js: true do 
    Given "user visits profile edit page"
    When "user updates profile basic info"
    When "user updates work experiences info"
    When "user updates education info"
    Then "user views profile updated"
  end



  def user_updates_work_experiences_info
    click_link "Parcours professionnel"
    click_link "Ajouter une expérience"
    find(".job-company-name").set("Natixis")
    find(".job-title").set("Contrôleur Financier")
    find(".job-location").set("Paris")
    select_date("2013,December,7", :from => "Date début")
  end


  def select_date(date, options = {})
    field = options[:from]
    base_id = find(:xpath, ".//label[contains(.,'#{field}')]")[:for]
    year, month, day = date.split(',')
    select year,  :from => "#{base_id}_1i"
    select month, :from => "#{base_id}_2i"
  end


end

当我 运行 测试时,我收到以下消息:

Failure/Error: select year,  :from => "#{base_id}_1i"
 Capybara::ElementNotFound:
   Unable to find select box "profile_experiences_attributes_1467917726232_started_at_2i_1i"

谢谢你们 ;)

看看你的 erb,我相信你的标签实际上会包含文本 'Date début' 而不是 'job-started-at'(需要实际生成的 HTML 来确认)。因此,按照目前的方式使用 select_date 方法,您需要调用

select_date("2013,December,7", :from => 'Date début')

另一种选择是更改 select_date 中 find 调用中的 selector/expression。无论哪种方式,我相信你会 运行 进入另一个问题,因为你指定 discard_day: true 我认为这意味着 select 从 select day, :from => "#{base_id}_3i" 没有日期字段会出错。您将需要更新 select_date 方法以使其更加灵活。

终于可以用了。我首先检查了 rails 控制台以查看变量 base 的值是什么:

profile_experiences_attributes_1467922079698_started_at_2i

我已经使用以下代码删除了变量的最后 3 个字符:

base_id = base[0...-3]

并更新了 select_date 方法:

 def select_date(date, options = {})
    field = options[:from]
    base = find(:xpath, ".//label[contains(.,'#{field}')]")[:for]
    base_id = base[0...-3]
    year, month, day = date.split(',')
    select month, :from => "#{base_id}_2i"
    select year,  :from => "#{base_id}_1i"
  end

到处都是绿灯!!!非常感谢你的帮助汤姆