Skip to content

Instantly share code, notes, and snippets.

@thisismydesign
Last active May 17, 2018 15:22
Show Gist options
  • Save thisismydesign/021310c596e72867e098097e49327e8e to your computer and use it in GitHub Desktop.
Save thisismydesign/021310c596e72867e098097e49327e8e to your computer and use it in GitHub Desktop.
ActiveRecord::Base is not `connected?` after successful `establish_connection`
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
a = gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "5.2.0"
gem "sqlite3", "1.3.13"
gem "rake", "~> 10.0"
gem "rspec", "~> 3.0"
end
require "active_record"
require "rspec"
sqlite3_config = {
"development" => {
"adapter" => "sqlite3",
"database" => "development.sqlite3"
}
}
RSpec.configure do |config|
config.formatter = "documentation"
end
RSpec.describe "connecting", :aggregate_failures do
it "is not connected by default" do
expect(ActiveRecord::Base.connected?).to be_falsey
expect{ ActiveRecord::Base.connection }.to raise_error(ActiveRecord::ConnectionNotEstablished)
expect{ ActiveRecord::Base.retrieve_connection }.to raise_error(ActiveRecord::ConnectionNotEstablished)
end
context "connected" do
before do
ActiveRecord::Base.establish_connection(sqlite3_config["development"])
# Issue closed, fix is to add `ActiveRecord::Base.connection` here
end
after do
ActiveRecord::Base.remove_connection
end
it "is connected" do
expect(ActiveRecord::Base.connected?).to be_truthy
expect(ActiveRecord::Base.connection).to be_kind_of(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
expect(ActiveRecord::Base.retrieve_connection).to eq(ActiveRecord::Base.connection)
end
end
end
$ rspec _rails#32918.rb
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
Using rake 10.5.0
Using concurrent-ruby 1.0.5
Using i18n 1.0.1
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.2.0
Using activemodel 5.2.0
Using arel 9.0.0
Using activerecord 5.2.0
Using bundler 1.16.0
Using diff-lcs 1.3
Using rspec-support 3.7.1
Using rspec-core 3.7.1
Using rspec-expectations 3.7.0
Using rspec-mocks 3.7.0
Using rspec 3.7.0
Using sqlite3 1.3.13
connecting
is not connected by default
connected
is connected (FAILED - 1)
Failures:
1) connecting connected is connected
Failure/Error: expect(ActiveRecord::Base.connected?).to be_truthy
expected: truthy value
got: false
# ./test.rb:50:in `block (3 levels) in <top (required)>'
Finished in 0.23324 seconds (files took 2.78 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./test.rb:49 # connecting connected is connected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment