2016-07-07 10 views
0

私は私のレールアプリに関するいくつかのテストをしています。私はいくつかのフォームについて宝石の繭を使用しています。私は "日付選択"をテストするいくつかのトラブルがあります。私が間違っていることを知りたいのですが、あなたの助けが大いに評価されるでしょう!RSpec - 日付コクーンの宝石

ここで私がこれまで行ってきたものを下記見つけてください:求人フォーム

ネストされたフィールドを(私は入力フィールド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を見てみると
+0

ラベルに "job-started-at"という文字が含まれていないと推測していますので、実際に生成されたHTMLをerbよりも追加してください。 –

答えて

0

最終的には動作しています。私は次のコードで変数の最後の3つの文字を削除した

profile_experiences_attributes_1467922079698_started_at_2i 

base_id = base[0...-3] 

そしてselect_dateを更新し、私は最初の変数baseの値が何であったかを表示するには、レールのコンソールをチェックします方法:

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 

どこでも緑色の光!あなたの援助を非常に感謝しています。Tom

+0

問題はありません。回答を受け入れることを忘れないでください。 –

0

を、私はあなたのラベルが実際に(それを確認するために、実際に生成された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メソッドを更新する必要があります。

+0

Tom Walpole assitance。それは間違いなくラベルに関連しています。 Capybara :: ElementNotFound: "profile_experiences_attributes_1467917726232_started_at_2i_1i"という選択ボックスが見つからないerb – purplecode

+0

@purplecodeで質問を更新します。誤ってセレクタをビルドしています - 通知_2i_1i - これは_1iのみである必要があります - 実際のHTMLを見て、ページに何が入っているのかを把握し、select_dateメソッドを正しく書き直す必要があります –

+0

今見て、あなたにアップデートを与えてください。あなたの素早い答えをありがとう – purplecode

関連する問題