【问题标题】:rspec DatabaseCleaner skip clean for example group metadata tagrspec DatabaseCleaner 跳过清理例如组元数据标签
【发布时间】:2013-06-19 12:51:07
【问题描述】:

如何标记示例组,以便在每个示例之间不清理数据库,而是在整个组之前和之后清理数据库?并且未标记的规范应该在每个示例之间清理数据库。

我想写:

describe 'my_dirty_group', :dont_clean do
  ...
end

所以我在我的 spec_helper.rb 中输入了:

  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:suite, dont_clean: true) do
    DatabaseCleaner.clean
  end

  config.after(:suite, dont_clean: true) do
    DatabaseCleaner.clean
  end

  config.before(:each, dont_clean: nil) do
    DatabaseCleaner.start
  end

  config.before(:each, dont_clean: nil) do
    DatabaseCleaner.clean
  end

问题在于,当未指定元数据标记时,spec_helper 中的 dont_clean: nil(或 false)块不会运行。是否有另一种方法可以在示例之间进行清理之前检查 :dont_clean 是否存在?

【问题讨论】:

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


    【解决方案1】:

    总结

    您可以在整个示例块上设置自定义元数据,然后使用 self.class.metadata 访问您的 RSpec 配置中的元数据以用于条件逻辑。

    代码

    使用这些 gem 版本:

    $ bundle exec gem list | grep -E '^rails |^rspec-core |^database'
    database_cleaner (1.4.0)
    rails (4.2.0)
    rspec-core (3.2.0)
    

    以下对我有用:

    文件:spec/spec_helper.rb

    RSpec.configure do |config|
    
      config.before(:suite) do
        DatabaseCleaner.strategy = :truncation
        DatabaseCleaner.clean_with(:truncation)
      end
    
      config.before(:all) do
        # Clean before each example group if clean_as_group is set
        if self.class.metadata[:clean_as_group]
          DatabaseCleaner.clean
        end
      end
    
      config.after(:all) do
        # Clean after each example group if clean_as_group is set
        if self.class.metadata[:clean_as_group]
          DatabaseCleaner.clean
        end
      end
    
      config.before(:each) do
        # Clean before each example unless clean_as_group is set
        unless self.class.metadata[:clean_as_group]
          DatabaseCleaner.start
        end
      end
    
      config.after(:each) do
        # Clean before each example unless clean_as_group is set
        unless self.class.metadata[:clean_as_group]
          DatabaseCleaner.clean
        end
      end
    
    end
    

    文件:spec/models/foo_spec.rb

    require 'spec_helper'
    
    describe 'a particular resource saved to the database', clean_as_group: true do
    
      it 'should initially be empty' do
        expect(Foo.count).to eq(0)
        foo = Foo.create()
      end
    
      it 'should NOT get cleaned between examples within a group' do
        expect(Foo.count).to eq(1)
      end
    
    end 
    
    describe 'that same resource again' do
    
      it 'should get cleaned between example groups' do
        expect(Foo.count).to eq(0)
        foo = Foo.create()
      end
    
      it 'should get cleaned between examples within a group in the absence of metadata' do
        expect(Foo.count).to eq(0)
      end
    
    end 
    

    【讨论】:

    • 我认为当describe 块中有多个contexts 时它将不起作用。请告知,在这种情况下应该怎么做,我们要在所有contexts之前清理。
    【解决方案2】:

    您可以查看块内的example.metadata,尽管我无法弄清楚如何为before(:suite) 执行此操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 2014-02-14
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多