2014-08-29 183 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