db 记录保存在控制器中并在集成规范中丢失 [英] db record saved in controller and lost in integration spec

查看:22
本文介绍了db 记录保存在控制器中并在集成规范中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们在 Rails 4.2 引擎中的规范使用的 gem.

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)

问题在于 CobOrderx::CobInfo.count 返回 0 而不是 1.调试时,@order 保存成功,CobOrderx::CobInfo.count 返回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.

但回到规范:

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

返回 false,因为 .count0.我们尝试在 rails_helper 中设置以下内容,但没有奏效:

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?

推荐答案

capybara 中搜索到创建的记录失败的最可能原因是工厂女孩或直接访问数据库使用单独的连接而不是水豚,因此在一个会话中创建的数据在另一个会话中不可用.要修复它,请使用 单连接补丁(把它放到规范/支持):

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

并且不要忘记将其包含在 rails_helper.rb 中.

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.

这篇关于db 记录保存在控制器中并在集成规范中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆