[英]Testing Sidekiq/ActiveJob - Job in ActiveJob but not Sidekiq
I monkey patched my user class (backed by devise) to use ActiveJob like so: 我修改了我的用户类(由devise支持)以使用ActiveJob,如下所示:
class User < ActiveRecord::Base
# Omitted
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end
end
I tried testing this with the following test: 我尝试使用以下测试进行测试:
class UserTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
def setup
@user = User.new(email: 'example@gmail.com', password: 'password',
password_confirmation: 'password')
end
test 'send_devise_notification queues into activejob' do
@user.save
assert_equal enqueued_jobs.size, 1 # This test passes
assert_equal Sidekiq::Extensions::DelayedMailer.jobs.size, 1 # This test fails
end
log/test.log looks like this: log / test.log看起来像这样:
------------------------------------------------------------------
UserTest: test_0011_send_devise_notification queues into activejob
------------------------------------------------------------------
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mUser Exists (0.4ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'example@gmail.com' LIMIT 1[0m
[1m[35mUser Load (0.3ms)[0m SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["confirmation_token", "62d1fac8e1319c29bfbe630aece15975963994b09746edbad65772e4598aa4d2"]]
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["email", "example@gmail.com"], ["encrypted_password", "$2a$04$b6/WjVWCthq.bKG1KN8JPeb3w8jz8oVWTU53BpTn90wIlTQ5cC2KO"], ["created_at", "2015-01-30 06:19:03.531310"], ["updated_at", "2015-01-30 06:19:03.531310"], ["confirmation_token", "62d1fac8e1319c29bfbe630aece15975963994b09746edbad65772e4598aa4d2"], ["confirmation_sent_at", "2015-01-30 06:19:03.730092"]]
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 9730d5a8-5dd4-4334-8290-e4e8e554715e) to Test(mailers) with arguments: "Devise::Mailer", "confirmation_instructions", "deliver_now", gid://swyp/User/232633312, "s7QVW_T2Aw8-ik6o1g2f", {}
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
[1m[35m (0.1ms)[0m BEGIN
enqueued_jobs inside of a debugger looks like this: 调试器内的enqueued_jobs如下所示:
[{:job=>ActionMailer::DeliveryJob,
:args=>["Devise::Mailer", "confirmation_instructions", "deliver_now", #<User id: 232633312, email: "example@gmail.com", encrypted_password: "$2a$04$E05.gR8oXSHQyk8hUOTBZ.PvswvHy2YYtTNXaha.wj2...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: "cba7134de82b6fde115fd3ca8e40975befeac299a1e619cdcb...", confirmed_at: nil, confirmation_sent_at: "2015-01-30 17:08:51", unconfirmed_email: nil, created_at: "2015-01-30 17:08:51", updated_at: "2015-01-30 17:08:51", first_name: nil, last_name: nil>, "QBLzzPLKVLy3znSZESBc", {}], :queue=>"mailers"}]
bin/rails c --environment test bin / rails c - 环境测试
irb(main):004:0> Rails.application.config.active_job.queue_adapter
=> :sidekiq
My Procfile looks like this: 我的Procfile看起来像这样:
web: bin/rails s
redis: redis-server
worker: bundle exec sidekiq -q default -q mailers
Anyone have any ideas? 有人有主意吗?
ActiveJob::TestHelper
doesn't actually pass the job to the adapter. ActiveJob::TestHelper
实际上并没有将作业传递给适配器。 If you remove it, it'll be an instance of ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper
. 如果你删除它,它将是一个ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper
。
In testing the default adapter is a TestAdapter
(see in your logs: [ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 9730d5a8-5dd4-4334-8290-e4e8e554715e) to Test(mailers) with arguments...). 在测试中,默认适配器是TestAdapter
(参见您的日志:[ActiveJob]排队ActionMailer :: DeliveryJob(作业ID:9730d5a8-5dd4-4334-8290-e4e8e554715e)到带有参数的测试(邮件程序)...)。
enqueued_jobs
and other helpers provided by ActiveJob::TestHelper
are querying the TestAdapter
and only works if you are using the TestAdapter in testing. enqueued_jobs
提供和其他助手ActiveJob::TestHelper
正在查询的TestAdapter
且仅当您使用的是测试TestAdapter工作。
So your first test passes as jobs are handled by the TestAdapter
but your 2nd test won't pass as the jobs from the TestAdapter
will never reach Sidekiq. 因此,当TestAdapter
处理作业时,您的第一次测试通过,但是第二次测试不会通过,因为来自TestAdapter
的作业永远不会到达Sidekiq。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.