简体   繁体   English

使用多个数据库测试Rails应用程序

[英]Testing a Rails Application with Multiple Databases

I have a rails application which uses two databases in production environment, Users and Process . 我有一个rails应用程序,它在生产环境中使用两个数据库, UsersProcess The Users model uses this customized ActiveRecord class: Users模型使用此自定义的ActiveRecord类:

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

class User < UserActiveRecord
    ...

Notice that the connection to a specific database is established depending on the environment. 请注意,根据环境建立与特定数据库的连接。 To simplify things, in the testing environment I have a single database with all the tables from the two production databases, and my database.yml file looks something like this: 为简化起见,在测试环境中,我有一个包含两个生产数据库中所有表的数据库,而我的database.yml文件看起来像这样:

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 runs fine in production, but when I run any test that refers to the User class, the test blocks at the exact point where User is used and nothing happens, it doesn't show any error, it doesn't exit, it just keeps waiting. 应用程序在生产中运行良好,但是当我运行任何引用User类的测试时,测试会在User使用的确切位置阻塞且没有任何反应,它不显示任何错误,它不会退出,它只是一直在等待。

I double-checked and the table USERS exists in the test database, in fact if I delete it manually I get the error that the table doesn't exists and when I create it again I get the same behavior reported in the previous paragraph. 我仔细检查并且表USERS存在于测试数据库中,实际上如果我手动删除它,我得到表不存在的错误,当我再次创建它时,我得到上一段中报告的相同行为。

I don't have a clue why this is happening, any idea on how can I solve this issue? 我不知道为什么会发生这种情况,我对如何解决这个问题有任何想法? or how can I debug it so that I can get to the root of the problem? 或者我如何调试它以便我可以找到问题的根源? If it helps, I'm using Ruby 1.9.3, Ruby on Rails 3.2.19 and PostgreSQL 9.3.5; 如果它有帮助,我使用Ruby 1.9.3,Ruby on Rails 3.2.19和PostgreSQL 9.3.5; any additional details will be posted as required. 任何其他详细信息将根据需要发布。

Have you tried explicitly stating that it's the same database? 您是否尝试过明确说明它是同一个数据库? Try editing your database.yml file to look like this: 尝试编辑您的database.yml文件,如下所示:

test: &default_test
    adapter: postgresql
    database: db_test
    host: localhost
    pool: ...
    timeout: ...
    username: ...
    password: ...

test_users_db:
    <<: *default_test 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM