【问题标题】:Testing Rails on Ruby with rspec -- it takes too long because of TRUNCATE tables on loading rspec使用 rspec 在 Ruby 上测试 Rails - 由于加载 rspec 时 TRUNCATE 表需要很长时间
【发布时间】:2021-03-04 13:09:19
【问题描述】:

我是 Ruby 和 Rails 的新手。我刚开始同时使用两者。

我正在开发的应用程序使用rspec 在 Ruby on Rails 中运行测试,并且在每一轮中,所有数据库表都被截断。拖尾log/test.log 显示许多带有TRUNCATE TABLE <table> 的行。我看到很多帖子使用Database cleaner gem 的配置如下:

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

这将跳过 TRUNCATE 表。或更全面的配置,例如:

config.before(:suite) do
  DatabaseCleaner.clean_with :transaction
end

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

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

config.after(:all) do

  DatabaseCleaner.clean
end

但它不起作用。

还有其他帖子建议在 Postgres 配置中设置fsync=off,其他教程也建议设置full_page_writes=off。但是我没有找到任何关于我正在使用的 DBMS 的信息。

MySQL有类似的配置吗?

【问题讨论】:

  • 您听说过viewcomponents 吗?它们易于测试且快速。
  • 我最近才了解它们,因为 rails 控制器中的非标准 ruby​​ 困扰着我。也许这会帮助您入门:copy-a-controller-class-instance-variables.
  • 关于视图组件的 cmets 似乎与所询问的内容无关。

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


【解决方案1】:

以下是我spec/rails_helper.rb的配置

RSpec.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.

  # References:
  #   https://relishapp.com/rspec/rspec-rails/docs/transactions
  #   http://stackoverflow.com/questions/21493970/databasecleaner-rspec-what-is-the-correct-configuration
  #   http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
  config.use_transactional_fixtures = false

  # https://github.com/DatabaseCleaner/database_cleaner#rspec-example
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:suite) do
    begin
      DatabaseCleaner.start
    ensure
      DatabaseCleaner.clean
    end
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end


  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!
end

我在多年前的项目中一直在使用上述内容。也许你可以试试这个。

您还可以查看我在探索正确使用 database_cleaner 时所记录的参考资料。顺便说一句,在我的应用程序中,我使用的是 database_cleaner,版本为 1.5.3。

希望对您有所帮助。谢谢。

【讨论】:

  • 感谢您的回答。不幸的是,它对我不起作用。有很多表,每次我运行 rspec 时,加载过程都会截断所有表,对于每个表,平均需要大约 7 秒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2013-10-15
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多