我的 Rails 4.2.6 应用程序使用两个数据库。我可以只使用一个进行测试吗?
My Rails 4.2.6 app uses two databases. Can I only use one for testing purposes?
我在 Rails 4.2.6 中有一个 Web 应用程序,它最初使用 MongoDB(通过 Mongoid 5)作为其主要数据库。但是,现在我需要连接到一个 MySQL 数据库,以便只为我的应用程序读取一些额外的数据。
至此,我的项目已经需要ActiveRecord,并且能够在开发环境中与上述数据库建立连接。我对这件事的配置文件是:
mongoid.yml
development:
clients:
default:
database: mongo_development
hosts:
- localhost:27017
options:
test:
clients:
default:
database: mongo_test
hosts:
- localhost:27017
options:
read:
mode: :primary
max_pool_size: 1
options:
raise_not_found_error: false
database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: mysql_development
pool: 5
username: user
password: pass
socket: /var/run/mysqld/mysqld.sock
当我 运行 我的测试套件时,问题就来了。我正在使用 RSpec 并执行 bundle exec rspec spec
产生以下错误消息:
/home/user/.rvm/gems/ruby-2.1.6@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'test' database is not configured. Available: ["development"] (ActiveRecord::AdapterNotSpecified)
ActiveRecord 抱怨没有要连接的测试数据库,但问题是我没有用于测试目的的 MySQL 数据库:我编写的规范仅与 Mongoid 模型交互。
我正在考虑创建一个空的 MySQL 数据库作为 hack,但我首先想知道我是否可以将 ActiveRecord 配置为不连接到测试数据库并仅使用 mongo_test
.
是否可以如我所愿?有人知道怎么做这个吗?
这里有两个解决方法
其中之一:为您的测试环境添加一个 sqlite 数据库。
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
或者:将您的自动加载从 application.rb
移动到特定于环境的配置,并将 activerecord
gem 移动到您的 [=] 上的 development
env 16=].
Gemfile
group :development do
gem 'activerecord'
end
config/application.rb:
首先:将其移动到 config/environments/test.rb
和 config/environments/development.rb
(记住要从 test
环境中排除哪些模型,即 activerecord
莫
config.autoload_paths += WHATEVER
删除 require 'rails/all'
行并要求在每个环境文件中也需要专门使用的框架。
我决定回答我自己的问题,因为提供的答案不符合我的需要。
经过多次试验和错误后,我能够避免连接到 test 环境中的 MySQL 数据库,以适应此 question post 中给出的信息。
我唯一需要做的就是将以下 gem 添加到我的 Gemfile:
group :test do
gem 'activerecord-nulldb-adapter'
end
这个gem在项目中安装了NullDB数据库适配器,这使得ActiveRecord无需定义真实的数据库就可以建立连接。
最后但同样重要的是,我更新了我的 config/database.yml 在 test 环境中添加了数据库连接的参数使用新适配器的:
test:
adapter: nulldb
encoding: utf8
reconnect: false
database: mysql_test
pool: 5
username: user
password: pass
socket: /var/run/mysqld/mysqld.sock
我在 Rails 4.2.6 中有一个 Web 应用程序,它最初使用 MongoDB(通过 Mongoid 5)作为其主要数据库。但是,现在我需要连接到一个 MySQL 数据库,以便只为我的应用程序读取一些额外的数据。
至此,我的项目已经需要ActiveRecord,并且能够在开发环境中与上述数据库建立连接。我对这件事的配置文件是:
mongoid.yml
development:
clients:
default:
database: mongo_development
hosts:
- localhost:27017
options:
test:
clients:
default:
database: mongo_test
hosts:
- localhost:27017
options:
read:
mode: :primary
max_pool_size: 1
options:
raise_not_found_error: false
database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: mysql_development
pool: 5
username: user
password: pass
socket: /var/run/mysqld/mysqld.sock
当我 运行 我的测试套件时,问题就来了。我正在使用 RSpec 并执行 bundle exec rspec spec
产生以下错误消息:
/home/user/.rvm/gems/ruby-2.1.6@global/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'test' database is not configured. Available: ["development"] (ActiveRecord::AdapterNotSpecified)
ActiveRecord 抱怨没有要连接的测试数据库,但问题是我没有用于测试目的的 MySQL 数据库:我编写的规范仅与 Mongoid 模型交互。
我正在考虑创建一个空的 MySQL 数据库作为 hack,但我首先想知道我是否可以将 ActiveRecord 配置为不连接到测试数据库并仅使用 mongo_test
.
是否可以如我所愿?有人知道怎么做这个吗?
这里有两个解决方法
其中之一:为您的测试环境添加一个 sqlite 数据库。
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
或者:将您的自动加载从 application.rb
移动到特定于环境的配置,并将 activerecord
gem 移动到您的 [=] 上的 development
env 16=].
Gemfile
group :development do
gem 'activerecord'
end
config/application.rb:
首先:将其移动到
config/environments/test.rb
和config/environments/development.rb
(记住要从test
环境中排除哪些模型,即activerecord
莫config.autoload_paths += WHATEVER
删除 require 'rails/all'
行并要求在每个环境文件中也需要专门使用的框架。
我决定回答我自己的问题,因为提供的答案不符合我的需要。
经过多次试验和错误后,我能够避免连接到 test 环境中的 MySQL 数据库,以适应此 question post 中给出的信息。
我唯一需要做的就是将以下 gem 添加到我的 Gemfile:
group :test do
gem 'activerecord-nulldb-adapter'
end
这个gem在项目中安装了NullDB数据库适配器,这使得ActiveRecord无需定义真实的数据库就可以建立连接。
最后但同样重要的是,我更新了我的 config/database.yml 在 test 环境中添加了数据库连接的参数使用新适配器的:
test:
adapter: nulldb
encoding: utf8
reconnect: false
database: mysql_test
pool: 5
username: user
password: pass
socket: /var/run/mysqld/mysqld.sock