postgresql_adapter async_exec 使用 Capybara + FactoryGirl 进行 rails rspec 测试时出错

postgresql_adapter async_exec error on rails rspec test using Capybara + FactoryGirl

当我尝试 运行 我在 rails 中的测试时遇到了这个奇怪的错误:

postgresql_adapter.rb:592:in `async_exec': PG::UndefinedTable: ERROR:  relation "users" does not exist (ActiveRecord::StatementInvalid)

我不知道这是否有帮助,但我的 spec_helper.rb 文件如下所示:

require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'database_cleaner'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist' 

require 'support/mailer_macros'
require 'support/test_helper'
require 'support/factory_girl_helper'

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new app, window_size: [1600, 1200], js_errors: false
end

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.include(FactoryGirlHelper)
  config.include(MailerMacros)
  config.include(TestHelper)
  config.before(:each) { reset_email }

  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

  Capybara.javascript_driver = :poltergeist
  config.include Capybara::DSL

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation) # moving to before :each doesn't help
    DatabaseCleaner.strategy = :truncation # moving to before :each doesn't help
  end

  config.around :each do |example| # refactoring as before/after with .start/.clean doesn't help
    DatabaseCleaner.cleaning { example.run }
  end
end

有人知道为什么会这样吗?浏览器中的应用程序似乎运行良好。

听起来您的测试数据库的模式不同步,您需要 运行 迁移或者 reset/recreate 它。

好的,我按照这里的答案想通了:FactoryGirl screws up rake db:migrate process

所以问题出在 factorygirl 身上。我将 Gemfile 更新为如下所示:

gem "factory_girl_rails", :require => false

然后还要加上这个:

require 'factory_girl_rails'

到我的 spec_helper.rb 文件,它解决了本地和 circleci 上的所有问题 :)