Russell Fine
2010-Mar-07 15:45 UTC
[rspec-users] How to test an app with multiple databases?
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 via http://www.ruby-forum.com/.
David Chelimsky
2010-Mar-07 15:50 UTC
[rspec-users] How to test an app with multiple databases?
On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists at 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
Russell Fine
2010-Mar-07 16:11 UTC
[rspec-users] How to test an app with multiple databases?
David Chelimsky wrote:> On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists at 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, > DavidThanks 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 via http://www.ruby-forum.com/.
David Chelimsky
2010-Mar-07 16:42 UTC
[rspec-users] How to test an app with multiple databases?
On Sun, Mar 7, 2010 at 10:11 AM, Russell Fine <lists at ruby-forum.com> wrote:> David Chelimsky wrote: >> On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists at 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.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?
David Chelimsky wrote:> On Sun, Mar 7, 2010 at 10:11 AM, Russell Fine <lists at ruby-forum.com> wrote: > >> David Chelimsky wrote: >> >>> On Sun, Mar 7, 2010 at 9:45 AM, Russell Fine <lists at 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. >> > > 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? >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
Juanma Cervera
2010-Jul-08 07:27 UTC
[rspec-users] How to test an app with multiple databases?
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 > endHow 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 via http://www.ruby-forum.com/.
David Chelimsky
2010-Jul-08 10:34 UTC
[rspec-users] How to test an app with multiple databases?
On Jul 8, 2010, at 2:27 AM, Juanma Cervera wrote:> 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.http://github.com/rspec/rspec-core/blob/master/Upgrade.markdown HTH, David