【问题标题】:How do I tell database_cleaner to not run in the middle of a particular group of rspec tests如何告诉 database_cleaner 不要在一组特定的 rspec 测试中运行
【发布时间】:2016-09-19 08:28:21
【问题描述】:

我有一组有序的测试,它们模仿用户操作并相互依赖。

如何将database_cleaner 配置为在这些有序测试过程中不清理数据库?

/spec/support/database_cleaner.rb:

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

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

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

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

测试样本:

RSpec.describe Conversation, type: :model, order: :defined do
  context 'by default' do
    before :context do
      @alice = create :user
      @bob = create :user
      @subject = 'subject'
      @body = 'body'
      @conversation = @alice.send_message(@bob, @subject, @body)
    end

    it 'should have the subject it was opened with' do
      expect(@conversation.subject).to eq @subject
    end

    it 'should have one message upon opening' do
      expect(@conversation.messages.count).to eq 1
    end

    it 'should be initiated by @alice' do
      expect(@conversation.initiator).to eq @alice
    end

    it 'should be received by @bob' do
      expect(@conversation.recipient).to eq @bob
    end

    it 'should have two messages when bob sends a new message' do
      @conversation.add_message(@bob, 'This is my second message')
      expect(@conversation.messages.count).to eq 2
    end
end

【问题讨论】:

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


    【解决方案1】:

    修复它。我的测试是随机运行的,除了这些之外,它们都有order: :defined 标签。所以我在support/database_cleaner.rbconfig.before 语句中添加了一个unless 条件。

    现在看起来像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2014-01-21
      • 1970-01-01
      相关资源
      最近更新 更多