RSpec - 选择日期茧宝石

时间:2016-07-07 15:14:22

标签: date testing rspec capybara apache-cocoon

我正在对我的rails应用程序进行一些测试。我正在使用有关某些形式的宝石茧。我在测试“日期选择”时遇到了一些麻烦。我想知道我做错了什么,你的帮助将不胜感激!

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

Job表单的嵌套字段(我正在尝试测试输入字段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和Capybara

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"

谢谢你们;)

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

终于有效了。我首先检查了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

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