gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:21:35 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com