Rspec test failure: ActiveRecord::ConnectionNotEstablish

1,916 views
Skip to first unread message

顾明

unread,
Mar 5, 2015, 10:06:57 PM3/5/15
to rs...@googlegroups.com
The problem didn't happened before, I do not knew if it caused by something upgrades.

Now all the tests have caused this problem, such as a simple:

require 'spec_helper'
describe 'some_thing' do
  it "returns the product price" do
    #expect(helper.product_price).to eq(100)
  end
end

Error list belows:
Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::ConnectionNotEstablished:
       ActiveRecord::ConnectionNotEstablished
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_handling.rb:87:in `connection'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:499:in `create_fixtures'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:984:in `load_fixtures'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:957:in `setup_fixtures'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/fixtures.rb:806:in `before_setup'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-rails-3.2.0/lib/rspec/rails/adapters.rb:110:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:333:in `instance_exec'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:333:in `instance_exec'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:387:in `execute_with'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:247:in `call'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/hooks.rb:474:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:343:in `with_around_example_hooks'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:174:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:548:in `block in run_examples'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `map'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `run_examples'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:512:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `map'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/configuration.rb:1526:in `with_suite_hooks'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:109:in `block in run_specs'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/reporter.rb:62:in `report'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:108:in `run_specs'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:86:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:70:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/runner.rb:38:in `invoke'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/exe/rspec:4:in `<top (required)>'
     # /Users/user_name/.rbenv/versions/2.1.5/bin/rspec:23:in `load'
     # /Users/user_name/.rbenv/versions/2.1.5/bin/rspec:23:in `<main>'
     #
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.

Help pls...

Al

unread,
Mar 6, 2015, 2:54:12 PM3/6/15
to rs...@googlegroups.com

顾明

unread,
Mar 8, 2015, 9:25:31 PM3/8/15
to rs...@googlegroups.com
Al, thanks for your reply.
But my app doesn't use database at all.
When I run a test, why it will run the example?
Form the error trace:
# /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:343:in `with_around_example_hooks'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example.rb:174:in `run'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:548:in `block in run_examples'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `map'
     # /Users/user_name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib/rspec/core/example_group.rb:544:in `run_examples'

在 2015年3月7日星期六 UTC+8上午3:54:12,Al写道:

Aaron Kromer

unread,
Mar 9, 2015, 1:17:56 PM3/9/15
to rs...@googlegroups.com

We do test against a simple sample application without ActiveRecord to catch related errors. Our current checks around when / if to load ActiveRecord related code is to check if ActiveRecord is already defined in the system. This can happen for several reasons:

  • You are still loading the ActiveRecord railtie
  • You are referencing ActiveRecord in an initializer which is loading it
  • Some other code in the code base is loading the ActiveRecord module (simply referencing it will cause Rails to autoload it)
  • Another gem in your system is loading ActiveRecord

The last bullet point above is the most common cause. I would start by checking your Gemfile for any gems which may list ActiveRecord as a gem dependency. This isn’t guaranteed to tell you the offending gem. It is likely Rails may be a dependency, or another related gem. Additionally, the gem may not even list AR as a dependency, but will reference ActiveRecord in the code. I would also look at any other gems which may have changed during the upgrade and slowly start to comment them out to see when the issue does / does not occur.

While it is possible there is an issue with rspec-rails somewhere, my initial review didn’t turn up anything obvious. I’m currently leaning towards another gem in your system loading it.


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/02123571-772f-4e12-bfcb-d555fc43f188%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

顾明

unread,
Mar 10, 2015, 3:28:48 AM3/10/15
to rs...@googlegroups.com
@ Aaron Kromer, thanks for your reply.
My app works well, it only raise that error when run rspec test.
I added gem 'factory_girl_rails', '4.2.1' recently, so I thought it is the gem which cause error.
But after uninstalling it or remove it from gem file, the error still be there.


My gem file details:


gem 'rails', '4.1.8'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'

gem 'therubyracer', platforms: :ruby
gem 'less-rails'
gem 'twitter-bootstrap-rails', github: 'seyhunak/twitter-bootstrap-rails', branch: 'bootstrap3'

gem 'nokogiri'
gem 'jquery-rails'
gem 'jbuilder', '~> 1.2'
gem 'haml-rails'
gem 'puma'
#gem 'dalli'

# export excel for report
gem 'zip-zip'
gem 'rubyzip', '1.0.0'
gem 'axlsx_rails'
gem 'aws-sdk'
gem 'tinymce-rails'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false, group: :doc

group :development do
  gem 'better_errors'
  gem 'pry'
  gem 'binding_of_caller'
  #gem 'ruby-prof'
end

group :test do
  gem 'webmock'
  gem 'vcr'
  gem 'capybara', '>= 2.0.3'
  gem 'selenium-webdriver'
  gem 'rspec-rails', '>= 2.12.2'
  gem 'faker'
  gem 'factory_girl_rails', '4.2.1'
end



在 2015年3月10日星期二 UTC+8上午1:17:56,Aaron Kromer写道:

Aaron Kromer

unread,
Mar 10, 2015, 1:36:26 PM3/10/15
to rs...@googlegroups.com

I setup a sample project with that Gemfile and I cannot reproduce the issue. Can you share your spec/spec_helper.rb file? Or point to a public repo which has it, or a gist.


顾明

unread,
Mar 15, 2015, 10:41:22 PM3/15/15
to rs...@googlegroups.com
thx, my spec_helper.rb :


# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
#require 'rspec/autorun'
require 'capybara/rspec'
require 'faker'
require 'capybara/rails'
require 'vcr'
require 'webmock/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb"),Rails.root.join("spec/features/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
#ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

#Capybara::Selenium::Driver.new(app,:browser => :chrome)

VCR.configure do |c|
  c.allow_http_connections_when_no_cassette = true
  c.cassette_library_dir = 'spec/cassettes' #this specified the directory for placing the record files
  c.hook_into :webmock
  c.configure_rspec_metadata!
  c.default_cassette_options = {
      :record => :new_episodes
  }
end

RSpec.configure do |config|
  config.include Capybara::DSL
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"

end

在 2015年3月11日星期三 UTC+8上午1:36:26,Aaron Kromer写道:
Reply all
Reply to author
Forward
0 new messages