为什么我的Rspec Feature测试不使用Capybara将项添加到测试数据库集合中?

时间:2015-07-29 12:21:30

标签: ruby-on-rails ruby rspec capybara

我的应用中有一个Sales_Opportunities页面。这些有很多Timeline_Events,可以通过Sales_Opportunity页面上的Bootstrap模式添加。它实际上适用于真实世界的应用程序(在我的机器上使用localhost服务器,我可以使功能正常工作),但测试本身没有 - 当我使用Byebug时,很清楚sales_opportunity.timeline_events数组是空的。

我的销售机会规格:

describe 'the Sales Opportunity view', :type => :feature do
  let(:company) {FactoryGirl.create(:company, :with_users)}
  let(:user) {company.organization.users.first}
  let!(:sales_opportunity) { FactoryGirl.create(:sales_opportunity, user: user, company: company )}

  describe 'the Sales Opportunity details view' do
    before(:each) do
      sign_in user
      visit sales_opportunity_path(sales_opportunity)
    end

    it 'has links to add a timeline event' do
      expect(page).to have_link('Add new task')
    end

    it 'adds a new timeline event', js: true do
      page.click_link('Add new task')
      sleep 1
      page.find('#timeline-event_modal').fill_in('Activity', with: "Sign the contract")
      page.find('#timeline-event_modal').fill_in('Due date', with: "2014/10/15")
      page.find('#timeline-event_modal').check "Completed"
      page.find('#timeline-event_modal').fill_in('timeline_event_event_notes', with: "Some example notes")
      page.find('#timeline-event_modal').click_button('Save')
      expect(current_path).to eq(sales_opportunity_path(sales_opportunity))
      byebug
      expect(page).to have_content('Sign the contract')
      expect(page).to have_content('2014-10-15')
      expect(page).to have_content('Yes')
      expect(page).to have_content('Some example notes')
    end
  end

工厂女孩代码(公司):

FactoryGirl.define do
  factory :company do
    company_name            "Test Company"
    existing_customer       "False"

    trait :with_users do
      association           :organization, :with_users
    end 
  end
end

工厂(组织):

FactoryGirl.define do
  sequence(:organization_name) { |n| "Organization - #{n}" }

  factory :organization do
    organization_name { generate(:organization_name) }

    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
  end
end

销售机会工厂:

FactoryGirl.define do
  sequence(:opportunity_name)    { |n| "Sales Oppotunity - #{n}" }

  factory :sales_opportunity do
    user
    opportunity_name     { generate(:opportunity_name) }
    close_date           "2014/12/12"
    sale_value           10000
    company_id           7
  end
end

如果它有帮助,我可以包含Controller和View代码,但如上所述,代码实际上在开发服务器上的应用程序中工作(时间轴事件已更新并插入到sales_opportunity显示页面上的表中)所以这是可以做的事情我设置测试的方式,设置我的工厂,或者我对工作方式的误解。

作为参考,我有其他代码在其他视图上的工作方式完全相同,工厂和rspec测试可以产生积极的结果。

上面突破的Byebug输出:

(byebug) sales_opportunity
#<SalesOpportunity id: 2535, close_date: "2014-12-12 00:00:00", user_id: 22124, created_at: "2015-07-29 12:06:35", updated_at: "2015-07-29 12:06:35", pipeline_status: 0, opportunity_name: "Sales Oppotunity - 15", company_id: 7650, sale_value: #<BigDecimal:7ffbbfe2e7b8,'0.1E5',9(27)>, swot_score: 0>

(byebug) company
#<Company id: 7650, company_name: "Test Company", existing_customer: false, created_at: "2015-07-29 12:06:35", updated_at: "2015-07-29 12:06:35", organization_id: 34613>

(byebug) sales_opportunity.timeline_events
#<ActiveRecord::Associations::CollectionProxy []>

(byebug) user
#<User id: 22124, name: "Test user 124", email: "test.user124@example.com", created_at: "2015-07-29 12:06:35", updated_at: "2015-07-29 12:06:35", password_digest: "$2a$04$bRkyvW1g9NtC9x4Skyicme6YDUUxDxA6Eg2iPoT9Xsy...", organization_id: 34613, remember_token: "47307ea19e1a63cf2ebdcc6a2cf8f763aef1561b", admin: false, password_reset_token: nil, password_reset_sent_at: nil>

我从rspec运行中得到以下错误:

Failure/Error: expect(page).to have_content('Sign the contract')
Rspec then lists the items on my page until the timeline_events table:
No data available in table Showing 0 to 0 of 0

所以很明显,一旦点击“保存”按钮,阵列仍然是空的,我们将返回测试套件中的sales_opportunity页面。

知道我做错了什么,或者我如何清理我的工厂/测试以使其正常工作?或者还有另一种方法可以确保功能在没有这种类型的测试的情况下工作吗?

0 个答案:

没有答案