Ruby-on-rails 多个数据库的测试和database_cleaner
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12390321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tests and database_cleaner for multiple databases
提问by HannesFostie
We have a Mailbox model which uses a secondary DB to which another mail app also connects. This is called using
我们有一个邮箱模型,它使用另一个邮件应用程序也连接到的辅助数据库。这称为使用
establish_connection :mail_database
Now, I suppose I could attempt to wrap that in an ifstatement so it uses a different connection for the test environment, but I'm wondering how I could keep this database clean, preferably using database_cleaner which we already use for the 'regular' database.
现在,我想我可以尝试将它包装在一个if语句中,以便它为测试环境使用不同的连接,但我想知道如何保持这个数据库干净,最好使用我们已经用于“常规”数据库的 database_cleaner .
I hope someone can nudge me in the right direction.
我希望有人能将我推向正确的方向。
采纳答案by Lars Schirrmeister
I have a rails3.2.10 application tested with rspec (2.12.0) and using database_cleaner (0.9.1 f4b44bb) having two database connections for mysql.
我有一个使用 rspec (2.12.0) 和使用 database_cleaner (0.9.1 f4b44bb) 测试的 rails3.2.10 应用程序,它有两个用于 mysql 的数据库连接。
These are set in the database.ymlsomething like:
这些在database.yml中设置如下:
test:
...
database: my_app_test
test_my_second_connection:
...
database: my_second_connection_test
The second database is connected in the model class with establish connection.
第二个数据库通过建立连接在模型类中连接。
I was able to use the following settings in my spec/spec_helper.rbfile according to the manual of database_cleaner:
根据database_cleaner的手册,我能够在我的spec/spec_helper.rb文件中使用以下设置:
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].start
end
config.after(:each) do
DatabaseCleaner.clean
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].clean
end
end
Additionally I have to use for some parts of the test suite non-transactional fixtures. So I had to add the meta information :db_truncate => truein my specs and the settings like this in order to change the strategy in test run:
此外,我必须使用测试套件的某些部分非事务性装置。所以我不得不在我的规范和这样的设置中添加元信息:db_truncate => true以更改测试运行中的策略:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
end
config.before(:each) do
if example.metadata[:db_truncation]
DatabaseCleaner.strategy = :truncation
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :truncation
else
DatabaseCleaner.start
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].start
end
end
config.after(:each) do
DatabaseCleaner.clean
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].clean
if example.metadata[:db_truncation]
DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record,{:connection => :test_my_second_connection}].strategy = :transaction
end
end
回答by Ganesh Shrivas
****set database to clean by database cleaner in rails:-****
config.before(:suite) do
DatabaseCleaner[:active_record, :connection => :test].clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner[:active_record, :connection => :test].strategy = :transaction
end
config.before(:each) do
DatabaseCleaner[:active_record, :connection => :test].start
end
config.after(:each) do
DatabaseCleaner[:active_record, :connection => :test].clean
end

