Testing a Rails application with multiple databases

I have a rails application that uses two databases in a production environment, Users and Process . The Users model uses this custom ActiveRecord class:

 class UserActiveRecord < ActiveRecord::Base establish_connection "#{Rails.env}_users_db" end class User < UserActiveRecord ... 

Please note that the connection to a specific database is established depending on the environment. To simplify the situation, in a test environment, I have one database with all tables from two production databases, and my database.yml file looks something like this:

 test: adapter: postgresql database: db_test host: localhost pool: ... timeout: ... username: ... password: ... test_users_db: adapter: postgresql database: db_test <--- Notice that this is the same database only in test env host: localhost pool: ... timeout: ... username: ... password: ... 

The application works fine, but when I run some test related to the User class, the test blocks are at the exact point where User used and nothing happens, it does not show any errors, it does not exit, it just waits.

I checked twice, and the Users table exists in the test database, in fact, if I delete it manually, I get an error that the table does not exist, and when I create it again, I get the same behavior, the previous paragraph.

I have no clue why this is happening, any idea on how I can solve this problem? or how can I debug it so that I can get to the root of the problem? If this helps, I use Ruby 1.9.3, Ruby on Rails 3.2.19 and PostgreSQL 9.3.5; any additional data will be posted as necessary.

+6
source share
1 answer

Have you tried to explicitly state that this is the same database? Try editing the database.yml file so that it looks like this:

 test: &default_test adapter: postgresql database: db_test host: localhost pool: ... timeout: ... username: ... password: ... test_users_db: <<: *default_test 
0
source

Source: https://habr.com/ru/post/984447/


All Articles