InesM InesM - 2 years ago 153
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
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| app, window_size: [1600, 1200], js_errors: false

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

config.before(:each) { reset_email }

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

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

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

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

Answer Source

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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download