系统测试产生间歇性故障

时间:2018-02-08 16:23:35

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

我们的测试在Rails 5.1中只是系统测试相互渗透。具体来说,我们在测试中一起运行时会出现“ActiveRecordNotFound”错误,但在单独运行时则不会。几乎就像在下一次测试已经开始之后发生了数据库清理调用一样。

宝石:

ruby '~> 2.5.0'
gem 'rails', '~> 5.1.4'
gem 'capybara'
gem 'chromedriver-helper'
gem 'rspec-rails'
gem 'selenium-webdriver'

Rails_helper:

Spec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.before(:each, type: :system) do
    driven_by :rack_test
  end

  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless
  end

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
end

示例失败:

RSpec.describe 'Banana Peeling', type: :system, js: true do
  it 'peels' do
    Timecop.freeze
    banana = create(:banana)
    visit(banana_path(banana.id))
    click_on 'start_peel'
    click_on 'save'
    expect(banana.reload.start_peel).not_to be nil
    Timecop.return
  end
end

ActiveRecord::RecordNotFound:
       Couldn't find Banana with 'id'=6257

1 个答案:

答案 0 :(得分:0)

水豚作者recommends against使用Timecop.freeze,而改为使用Timecop.travel