【问题标题】:Tell Rspec - don't delete my test_db when before running tests告诉 Rspec - 在运行测试之前不要删除我的 test_db
【发布时间】:2014-08-18 13:50:35
【问题描述】:

我填充了一个我想对其运行 rspec 测试的数据库。

Rspec 似乎在开始测试之前删除了数据库。如何告诉 Rspec 不要删除这个 test_db?

我在 Rails / Rspec 环境之外填充数据库。建立建立数据库所需的所有工厂对于项目范围来说是不切实际的,但我想针对现有数据库进行测试。

【问题讨论】:

    标签: ruby-on-rails rspec rspec-rails


    【解决方案1】:

    您可以使用database cleaner gem 管理清理您的数据库,并且文档建议将rspec 配置为link,但您可以进行如下更改:

    RSpec.configure do |config|
    
      config.before(:suite) do
        DatabaseCleaner.strategy = :transaction
        DatabaseCleaner.clean_with(:truncation)
      end
    end
    

    但是,我更喜欢在场景之前在测试环境中创建您的数据库,例如:

    describe "specific name for this scenario" do
      before do
        @object = create(:object) # using factory girl as example
      end
    end
    

    因此,每次运行测试用例时都会生成此数据,请阅读有关creating data for rspec-test 的更多信息。

    【讨论】:

    • 构建数据库所需的所有工厂对于项目范围来说是不切实际的,但我想针对现有数据库进行测试。
    • 我希望 test_db 在测试前后不被删除。我会维护它。
    • 好的,您可以使用数据库清理器来管理何时清理您的数据?不适合你?
    • 数据库清理器如何使用 Rspec?当您说 DatabaseCleaner.strategy = :transaction 和 DatabaseCleaner.clean_with(:truncation) 时,是否会覆盖我的 Rspec 配置块中的 config.use_transactional_fixtures = true 之类的语句?
    • 有人对这个问题有简单的答案吗?我有一个现有的数据库,我不希望 rspec 在运行测试之前清除它。但是在运行测试后让 rspec 重置数据库会很好。
    【解决方案2】:

    我使用了这个问题中给出的建议:Rspec don't delete 2 specific tables

    在 spec_helper.rb 中:

    注释掉对 truncate_all_tables 的调用。

    然后换行:

    config.use_transactional_fixtures = true
    

    config.use_transactional_fixtures = false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多