user938363
user938363

Reputation: 10360

db record saved in controller and lost in integration spec

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?

Upvotes: 0

Views: 186

Answers (1)

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16507

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.

Upvotes: 2

Related Questions