这里是我们在Rails 4.2引擎中的规范所使用的宝石。
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' 下面是一个用于创建订单记录的集成规范:
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 returns 0而不是1。在调试中,@order被成功保存,CobOrderx::CobInfo.count返回1。
但在规范中:
expect(CobOrderx::CobInfo.count).to eq(1)返回false,因为.count是0。我们试图在rails_helper中设置以下内容,但没有工作:
config.use_transactional_fixtures = false问题似乎是在规格(水豚),我们不知道为什么。什么会导致数据库记录在集成测试中丢失?
发布于 2016-05-01 21:50:12
未能在capybara中搜索创建的记录的最可能原因是,工厂女孩或直接访问db使用的连接与capybara不同,因此在一个会话中创建的数据在另一个会话中不可用。若要修复它,请使用单连接贴片 (将其放入单连接贴片)
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.
如果它不能帮助您,您可以尝试在有关该主题的其他讨论中找到答案,这是可用的这里。
https://stackoverflow.com/questions/36972228
复制相似问题