0

Here is the gems used for our spec in Rails 4.2 engine.

  s.add_development_dependency "sqlite3"
  s.add_development_dependency "rspec-rails", ">= 3.2.0"
  s.add_development_dependency "factory_girl_rails", '~>4.5'
  s.add_development_dependency 'capybara'
  s.add_development_dependency 'launchy'  

Here is a integration spec for create an order record:

  visit multi_item_orderx.orders_path(customer_id: @kustomer.id)
  click_link 'New Cob Order'
  expect(page).to have_content('New Order')
  fill_in 'order_order_date', :with => 10.days.ago
  fill_in 'order_order_total', :with => 50
  fill_in 'order_cob_info_bonding_product', :with => 'a bonding prod'
  fill_in 'order_cob_info_name', :with => 'storage'
  fill_in 'order_cob_info_qty', :with => 10
  fill_in 'order_cob_info_unit_price', :with => 10
  click_button 'Save'
  expect(CobOrderx::CobInfo.count).to eq(1)

The problem is that CobOrderx::CobInfo.count returns 0 instead of 1. In debug, @order is saved successfully and CobOrderx::CobInfo.count returns 1.

But back in spec:

expect(CobOrderx::CobInfo.count).to eq(1)

returns false since .count is 0. We tried to set the following in rails_helper and it did not work:

config.use_transactional_fixtures = false

The problem seems to be with the spec (capybara) and we are not sure why. What could cause the db record lost in integration test?

user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

2

The most probable reason in fail to search a created record in capybara is that factory girl or direct access to db uses separate connection than capybara, so and data created in one session isn't available in another. To fix it use the single connection patch (put it to spec/support):

require 'active_record'

class ActiveRecord::Base
   mattr_accessor :shared_connection
   @@shared_connection = nil

   def self.connection
      @@shared_connection ||= retrieve_connection
   end
end

And don't forget to include it into rails_helper.rb.

In case if it can't help you, you may try to find an answer in the additional discussions on the topic, which is available here.

Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • Interesting!. Do you mean to create a file (any name?) with the code in your post under spec/support (there is no support under our spec subdir)? – user938363 May 01 '16 at 21:58
  • @user938363 yes, and dont forget include it into rails_helper. – Малъ Скрылевъ May 01 '16 at 21:58
  • `Dir[File.join(ENGINE_RAILS_ROOT, 'spec/support/**/*.rb')].each {|f| require f }` this is line in `rails_helper`. Is this the right way to include a file under spec/support? A file `single_connection_patch.rb` is created under spec/support. It seems not pickup. – user938363 May 02 '16 at 00:13