ruby-on-rails - 处理后台作业的 Ruby RSpec 最佳实践是什么?我正在做一些不必要的复杂事情吗?

标签 ruby-on-rails ruby testing redis rspec

我不确定我是否在某个地方有错误,或者只是没有使用良好的做法。假设我有以下任一条件:

class ThingMailer < ApplicationMailer
  def notify_of_thing
    mail(subject: 'Thing has happened')
  end
end

# ... and elsewhere...

class ThingDoer
  def do_thing
    ThingMailer.notify_of_thing.deliver_later(wait: 30.seconds)
  end
end

class ThingWorker
  include Sidekiq::Worker

  def perform(name)
    some_model.update(name: name)
  end
end

# ... and elsewhere...

class ThingPerformer
  def perform_thing
    ThingWorker.perform_in(30.seconds, 'Bob')
  end
end

在其他地方我有一个功能测试(或其他高级测试),在正常情况下,这会导致 ThingPerformer#perform_thingThingDoer#do_thing 成为叫。在测试套件中处理它们的最佳实践是什么?

在我的实际测试套件中,如果我不只是停止其中一种线程启动方法,并且如果我在测试运行时没有在后台运行 Redis,我会得到错误 Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)

config/environments/production.rb 中,我们指定缓存存储:

config.cache_store = :redis_store, ENV['REDIS_URL'], { expires_in: 90.minutes }

但是我们的 config/environments/test.rb,我们指定应用程序不应该执行缓存(虽然这可能不起作用,也许只是修复导致这个问题的任何东西就是答案第一个问题?)

这是 test.rb 文件:

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = false

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true
  config.public_file_server.headers = {
    'Cache-Control' => 'public, max-age=3600'
  }

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false
  config.action_mailer.perform_caching = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test
  # config.active_job.queue_adapter = :test

  # Fix the order in which test cases are executed.
  # config.active_support.test_order = :sorted

  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  # Raises error for missing translations
  config.action_view.raise_on_missing_translations = true

  config.cache_store = :null_store
end

最佳答案

Sidekiq 提供了一个 testing guide .

如果你想测试作业的行为,使用Sidekiq::Testing.inline!。如果您只想检查代码而不测试作业,请使用 Sidekiq::Testing.fake! 并检查作业是否已入队。

关于ruby-on-rails - 处理后台作业的 Ruby RSpec 最佳实践是什么?我正在做一些不必要的复杂事情吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161310/

相关文章:

mysql - rails : auto increment column with scope

ruby-on-rails - 如何使用 CarrierWave 在 rails 中上传动画 GIF?

ruby-on-rails - 未定义的方法 `receive_message_chain'

java - 如何为下载管理器编写测试驱动的 Java 类?

ruby-on-rails - 是否值得测试诸如范围之类的低级代码?

php - api.example.com 比 example.com/api 好吗?

javascript - 这个数组定义到底做了什么?

ruby-on-rails - 在 JavaScript 中访问 c​​urrent_user 帮助程序

scala - 当未过滤的 Netty 服务器实际关闭时如何得到通知?

使用测试工具调试 Forth