我创建了一些用户创建销售机会的代码,这些机会与某个公司相关联。当用户必须手动输入公司的ID时,我设置了一些工作正常的代码,然后对其进行了更改,以便表单显示与该用户组织相关的所有公司的列表(用户belongs_to组织,公司) belongs_to organization,sales_opportunity belongs_to User and Company)。
这导致我的Rspec / Capybara测试失败,并显示以下错误消息:
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 %>
如果您认为必要,我可以包含代码的其他部分,但从我目前的猜测来看,公司似乎正在使用&#34; Let&#34; block与我的组织/用户无关,或者表单由于某种原因无法识别该公司。我无法弄清楚我在这里做错了什么 - 你能帮忙吗?
答案 0 :(得分: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: "email2@example.com")
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进行构建。