Permalink
Browse files

Fix case where Rails engine testing may result in a not yet connected…

… database.

In the project I was working on, I was encountering a point where the
ActiveRecord::Base.connection_pool had not yet been connected. This raised an
exception. This patch catches the current exception that is thrown.

I'm not entirely satisified with the exception handling, as it relates to the
inner working of ActiveRecord::Base; However, NullDB is meant to be an
ActiveRecord adaptor, so this is perhaps germain to the test suite.
  • Loading branch information...
jeremyf committed Jul 5, 2012
1 parent 6a364c5 commit c8f0ae5afdac730a0a1ea4279e47920c2bca1c87
Showing with 10 additions and 1 deletion.
  1. +4 −1 lib/nulldb/core.rb
  2. +6 −0 spec/nulldb_spec.rb
View
@@ -17,7 +17,10 @@ def configuration
end
def nullify(options={})
- @prev_connection = ActiveRecord::Base.connection_pool.try(:spec)
+ begin
+ @prev_connection = ActiveRecord::Base.connection_pool.try(:spec)
+ rescue ActiveRecord::ConnectionNotEstablished
+ end
ActiveRecord::Base.establish_connection(options.merge(:adapter => :nulldb))
end
View
@@ -232,6 +232,7 @@ def should_have_column(klass, col_name, col_type)
col.should_not be_nil
col.type.should == col_type
end
+
it "should support adding indexes" do
Employee.connection.indexes('employees').size.should == 2
@@ -250,6 +251,11 @@ def should_have_column(klass, col_name, col_type)
it "should support custom index names" do
Employee.connection.indexes('employees_widgets').first.name.should == 'my_index'
end
+
+ it 'should handle ActiveRecord::ConnectionNotEstablished' do
+ ActiveRecord::Base.should_receive(:connection_pool).and_raise(ActiveRecord::ConnectionNotEstablished)
+ lambda { NullDB.nullify }.should_not raise_error(ActiveRecord::ConnectionNotEstablished)
+ end
end
# need a fallback db for contextual nullification

0 comments on commit c8f0ae5

Please sign in to comment.