我在用户创建了与某个公司关联的销售机会时创建了一些代码。我设置了一些工作正常的代码,当用户必须手动输入公司的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”块创建的公司与我的组织/用户没有关联,或者表单由于某种原因无法识别该公司。我无法弄清楚我在这里做错了什么 - 你能帮忙吗?
哦,仅供参考,代码在浏览器中工作正常 - 所以它是测试,而不是底层代码,据我所知。 – Zoinks10 2014-08-29 09:35:24
将'gem launchy'添加到您的Gemfile中,而不是在spec文件中的'visit'后添加'save_and_open_page'。它会在您的浏览器中打开该页面,您可以验证该元素是否真的存在。不要相信它应该在那里,但检查。 – pawel7318 2014-08-29 22:25:55