database_cleaner,factory_girl和多个数据库 - 配置

时间:2015-01-15 16:09:41

标签: ruby-on-rails ruby rspec factory-bot multiple-databases

目前尝试使用capybara / rspec / factory girl测试多个db,尽管我的数据库清除有问题。

查询错误:

': Mysql2::Error: Duplicate entry '1503' for key 'PRIMARY': INSERT INTO `users`

Facilities_spec.rb

feature "User with facilities" do
  @current_user = FactoryGirl.create(:user_with_facility)   
  scenario 'A user can perform a walk-through', :js => true do
    login_as
    visit '/'
    expect(page).to have_text "Our records indicate that you have access to 1 facilities:"
    ...
  end

  @current_user = FactoryGirl.create(:user_with_facility)   
  scenario 'The quick-form requires first_name, last_name, and dob', :js => true do
    login_as
    visit '/'
    expect(page).to have_text "Our records indicate that you have access to 1 facilities:"
    ...
  end
end

rails_helper.rb

cleaner = DatabaseCleaner[:active_record,{:connection => :emp_portal_test}]
rt_cleaner = DatabaseCleaner[:active_record, {connection: :test_rt_treats}]

RSpec.configure do |config|
 config.include Capybara::DSL
 config.include FactoryGirl::Syntax::Methods

 config.use_transactional_fixtures = false

 config.before(:suite) do 
   cleaner.strategy = :truncation
   rt_cleaner.strategy = :truncation     
 end

 config.before(:each) do 
   cleaner.strategy = :truncation
   rt_cleaner.strategy = :truncation
   cleaner.start
   rt_cleaner.start
 end

config.before(:each, :js => true) do
  cleaner.strategy = :truncation
  rt_cleaner.strategy = :truncation
end

config.after(:each) do 
  cleaner.clean
  rt_cleaner.clean   
end

user_factory.rb

FactoryGirl.define do
 factory :user do
  id 33065
  first_name "Andrew"
  last_name  "Larson"
  select_id "al44096"
    factory :user_with_facility do
      after(:create) do |user|
        user.facility_assignments << create(:facility)
      end
    end
  end

factory :facility do
    id 1550
    ref_select_id 1550
    status -1
    name "St. Paul's Home & Apartments"
    name_internal "St Paul's Home"
    dept_id "R51"
    ...
end

当我在这个测试环境中创建一个新患者时,它会在我下次使用之前被擦除,尽管我不能在我的代码中使用相同的current_user。

1 个答案:

答案 0 :(得分:0)

好几件......

  1. 我的一个数据库&#39;表没有自动增量功能,因为它的ID是一个问题,所以我通过...添加了自动增量到表...

    ALTER TABLE document MODIFY COLUMN document_id INT auto_increment
    
  2. 解决此问题后,我的数据库清理工作正常。

  3. 奖励:我不知道,但要在我需要的两种情况下访问@current_user

    background do
      @current_user = FactoryGirl.create(:user_with_facility)   
    end
    
  4. 直到现在我还没有看到这个,而且很棒。

    2 examples, 0 failures