使用 rails gem database_cleaner 当 rSpec 测试和使用 activerecord-sqlserver-adapter

Using the rails gem database_cleaner when rSpec testing and using activerecord-sqlserver-adapter

我正在使用 activerecord-sqlserver-adapter gem to connect to a 2008 SQL Server Database within my rails application. Please note that I do connect to multiple db's throughout my application using 'establish_connection'. I use rSpec to test my application and I want to remove the entries from the DB that rSpec creates. I googled around and found that the database_cleaner gem 可以解决我的问题,所以我包含了 Gem 并设置了我的 railsappname/spec/spec_helper.rb:

require 'database_cleaner'

RSpec.configure do |config|

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    FactoryGirl.reload
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

我还将以下行更新为 false

config.use_transactional_fixtures = false

到我的 rails_helper.rb RSpec.configure 块,因为那是该行最初所在的位置。

我也尝试过将我的策略更改为 :truncation,但没有任何效果。数据仍在插入数据库中,测试完成后不会被删除。这是对 activerecord-sqlserver-adapter 的限制吗?是否有解决方法?这个问题是由我在我的应用程序中连接到多个数据库引起的吗?我在谷歌上搜索了一段时间,没有看到其他人遇到同样的问题,所以非常感谢任何建议!

感谢您的提醒,但您的堆栈溢出问题实际上与 SQL 服务器或适配器无关。它实际上是您的软件组合方式的副产品。我敢打赌这一切都与你如何建立联系有关。查看有关适配器的评论。

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/173#issuecomment-4427121

基本上,您要确保只有一个支持连接的模型,以便您可以管理它。大多数 gem 只知道 ActiveRecord::Base。因此,如果您的应用程序包含许多数据库,请确保您完成了所有需要做的事情。这很棘手。如此之多,以至于我什至写了一篇关于它的博客文章。你应该读一读:)

http://technology.customink.com/blog/2015/06/22/rails-multi-database-best-practices-roundup/

最后...这纯粹是主观的...但我是 Rails 事务固定装置的忠实拥护者,而不是 运行 您使用空数据库进行的测试。我的建议:

同样,纯属主观,但我希望最后一点有所帮助。干杯。