Forum: RSpec How to test an app with multiple databases?

Posted by Russell Fine (misterfine)
on 2010-03-07 16:45
Our app connects to two databases.  The main database (through
ActiveRecord::Base) is automatically cleared before each test.  How do I
force the clear of the secondary database as well ?

Thanks in advance...

Russell
Posted by David Chelimsky (Guest)
on 2010-03-07 17:02
(Received via mailing list)
On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists@ruby-forum.com> 
wrote:
> Our app connects to two databases.  The main database (through
> ActiveRecord::Base) is automatically cleared before each test.  How do I
> force the clear of the secondary database as well ?

There's no implicit support for this, so you'd have to do something
manually in a before(:each) block. You can do that in the
configuration (usually in spec_helper):

Spec::Runner.configure do |c|
  c.before(:each) do
    # clear out 2ndary db
  end
end

HTH,
David
Posted by Russell Fine (misterfine)
on 2010-03-07 17:11
David Chelimsky wrote:
> On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists@ruby-forum.com> 
> wrote:
>> Our app connects to two databases. �The main database (through
>> ActiveRecord::Base) is automatically cleared before each test. �How do I
>> force the clear of the secondary database as well ?
> 
> There's no implicit support for this, so you'd have to do something
> manually in a before(:each) block. You can do that in the
> configuration (usually in spec_helper):
> 
> Spec::Runner.configure do |c|
>   c.before(:each) do
>     # clear out 2ndary db
>   end
> end
> 
> HTH,
> David

Thanks for the quick reply.  Do you happen to know where in the 
framework I would call to clear out the db?  I can obviously do it 
myself by hand by just deleting all elements, but I'm worried that the 
testing framework may perform some unique actions that differ from what 
I would do.

Russell
Posted by David Chelimsky (Guest)
on 2010-03-07 18:11
(Received via mailing list)
On Sun, Mar 7, 2010 at 10:11 AM, Russell Fine <lists@ruby-forum.com> 
wrote:
>>
> framework I would call to clear out the db?  I can obviously do it
> myself by hand by just deleting all elements, but I'm worried that the
> testing framework may perform some unique actions that differ from what
> I would do.

rspec-rails just wraps the rails testing framework facilities, so
whatever you're looking for is going to be found in the rails code.

I'm not sure how the database_cleaner gem handles multiple databases,
but you might find your answer there.

Can anybody else point Russell in the right direction?
Posted by Ben Mabey (mabes)
on 2010-03-08 09:55
(Received via mailing list)
David Chelimsky wrote:
>>>>         
>>> HTH,
> whatever you're looking for is going to be found in the rails code.
>
> I'm not sure how the database_cleaner gem handles multiple databases,
> but you might find your answer there.
>
> Can anybody else point Russell in the right direction?
>   

DatabaseCleaner doesn't support multiple databases ATM.  I have had some
discussions with people about adding support for multiple database types
(i.e. AR and MongoMapper in the same app) but haven't thought about
multiple DB connections for the same adapter.  Off the top of my head I
don't think it should be too difficult to do for AR.  It sounds like you
are using the standard rails transactional rollbacks for your tests
right now.  In order to clear out your second database in a similar
fashion (with transactions) I would try something like this in your
spec_helper:

require 'database_cleaner'
DatabaseCleaner.strategy = :transaction

Spec::Runner.configure do |c|
  c.before(:each) do
    ActiveRecord::Base::establish_connection :secondary_db
    DatabaseCleaner.start
    ActiveRecord::Base::establish_connection :primary_db
  end

  c.after(:each) do
    ActiveRecord::Base::establish_connection :secondary_db
    DatabaseCleaner.clean
    ActiveRecord::Base::establish_connection :primary_db

  end
end


I haven't tried the above code but it seems correct.  Give it a try and 
let me know if it works.  If it does I could add support to 
DatabaseCleaner so you can select which AR DB connections you want to 
clean.

HTH,
Ben
Posted by Juanma Cervera (jmcervera)
on 2010-07-08 09:27
Ben Mabey wrote:
> Spec::Runner.configure do |c|
>   c.before(:each) do
>     ActiveRecord::Base::establish_connection :secondary_db
>     DatabaseCleaner.start
>     ActiveRecord::Base::establish_connection :primary_db
>   end
> 
>   c.after(:each) do
>     ActiveRecord::Base::establish_connection :secondary_db
>     DatabaseCleaner.clean
>     ActiveRecord::Base::establish_connection :primary_db
> 
>   end
> end

How would I translate this code for rspec 2.0.0.beta.15?
I am using Rspec::Core::Runner but it has no configure method.

Thanks
Posted by David Chelimsky (Guest)
on 2010-07-08 12:42
(Received via mailing list)
On Jul 8, 2010, at 2:27 AM, Juanma Cervera wrote:

>>    DatabaseCleaner.clean
>>    ActiveRecord::Base::establish_connection :primary_db
>> 
>>  end
>> end
> 
> How would I translate this code for rspec 2.0.0.beta.15?
> I am using Rspec::Core::Runner but it has no configure method.

http://github.com/rspec/rspec-core/blob/master/Upgrade.markdown

HTH,
David
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.