InesM InesM - 5 months ago 39
Ruby Question

postgresql_adapter async_exec error on rails rspec test using Capybara + FactoryGirl

I'm getting this strange error when I try to run my test in rails:

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


I don't know if this helps but my
spec_helper.rb
file looks like this:

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


Anyone has any idea why this is happening? The application in the browser seems to be working fine.

Answer

Ok I figured it out following the answer here: FactoryGirl screws up rake db:migrate process

So the problem was coming from factorygirl. I updated my Gemfile to look like this:

gem "factory_girl_rails", :require => false

And then also add this:

require 'factory_girl_rails'

to my spec_helper.rb file and that fixed all the issues both localy and on circleci :)

Comments