2014-08-29 181 views
0

我在用戶創建了與某個公司關聯的銷售機會時創建了一些代碼。我設置了一些工作正常的代碼,當用戶必須手動輸入公司的ID時,它才能正常工作,然後對其進行更改,以便表單顯示與該用戶組織關聯的所有公司的列表(用戶belongs_to組織,company belongs_to organization,sales_opportunity屬於用戶和公司)。從水滴中選擇水豚時發現元素錯誤

這引起了我的Rspec的/水豚測試失敗,出現以下錯誤信息:

Failure/Error: page.select "Test Co", :from => "Company" 
Capybara::ElementNotFound: 
    Unable to find option "Test Co" 

相關測試:

describe "sales opportunities" do 
    let(:organization) { FactoryGirl.create(:organization, :name_one, :with_users)} 
    let(:company) {organization.companies.create(company_name: "Test Co", organization_id: organization.id, existing_customer: true)} 
    let(:user) {organization.users.first} 
    before do 
    sign_in user 
    visit user_path(user) 
    end 
it 'has links to add a new sales opportunity' do 
    expect(page).to have_link('Add sales opportunity', href: new_user_sales_opportunity_path(user_id: user.id)) 
end 

it 'adds a new sales opportunity' do 
page.click_link('Add sales opportunity') 
page.fill_in('Opportunity name', with: "Capybara Opportunity") 
page.fill_in('Close date', with: "2014/12/18") 
page.fill_in('Sale value', with: 20000) 
page.select "Test Co", :from => "Company" 
page.click_button('Save') 
expect(current_path).to eq(user_path(user)) 
expect(page).to have_content('Capybara Opportunity') 
expect(page).to have_content('20000') 
expect(page).to have_content('2014-12-18') 
end 

選擇一個公司的表單字段:

 <%= f.label :company_id %><br> 
    <%= f.collection_select :company_id, @user.organization.companies(:company_name), :id, :company_name %> 

我可以包含代碼的其他部分,如果你認爲他們是必要的,但從我curre nt猜想看起來我用「Let」塊創建的公司與我的組織/用戶沒有關聯,或者表單由於某種原因無法識別該公司。我無法弄清楚我在這裏做錯了什麼 - 你能幫忙嗎?

+0

哦,僅供參考,代碼在瀏覽器中工作正常 - 所以它是測試,而不是底層代碼,據我所知。 – Zoinks10 2014-08-29 09:35:24

+0

將'gem launchy'添加到您的Gemfile中,而不是在spec文件中的'visit'後添加'save_and_open_page'。它會在您的瀏覽器中打開該頁面,您可以驗證該元素是否真的存在。不要相信它應該在那裏,但檢查。 – pawel7318 2014-08-29 22:25:55

回答

0

好的,所以我解決了這個問題 - 這與我在測試套件中構建公司的方式有關。相反,如上面做的,我把它變成一個工廠來代替:

FactoryGirl.define do 

factory :organization do 
    trait :name_one do 
    organization_name  "New Example Org" 
    end 

    trait :name_two do 
     organization_name "Any Wrong Org" 
    end 
    trait :with_users do 
    before(:create) do |organization| 
     organization.users << FactoryGirl.build(:user) 
     organization.users << FactoryGirl.build(:user, name: "Second User", email: "[email protected]") 
    end 
end 

和:

factory :company do 
    company_name   "Test Company" 
    existing_customer  "False" 
    association    :organization, :name_one, :with_users 
end 

然後叫其作爲RSpec的測試如下:

describe "sales opportunities" do 
    let(:company) {FactoryGirl.create(:company)} 
    let(:user) {company.organization.users.first} 
    before do 
    sign_in user 
    visit user_path(user) 
    end 
it 'has links to add a new sales opportunity' do 
    expect(page).to have_link('Add sales opportunity', href: new_user_sales_opportunity_path(user_id: user.id)) 
end 

it 'adds a new sales opportunity' do 
page.click_link('Add sales opportunity') 
page.fill_in('Opportunity name', with: "Capybara Opportunity") 
page.fill_in('Close date', with: "2014/12/18") 
page.fill_in('Sale value', with: 20000) 
page.select "Test Company", :from => "Company" 
page.click_button('Save') 
expect(current_path).to eq(user_path(user)) 
expect(page).to have_content('Capybara Opportunity') 
expect(page).to have_content('20000') 
expect(page).to have_content('2014-12-18') 
end 

這所有的測試都是綠色的,所以雖然我不確定我最初做錯了什麼,但答案是使用FactoryGirl來構建。

+0

有趣的是,我讀過一些人認爲完全相反 - 使用這種寶石增加了複雜性。 – BKSpurgeon 2016-03-31 00:04:05