简体   繁体   English

如何在Rails中测试`perform_now`作业

[英]How can I test `perform_now` job in Rails

Recently on my current project, I noticed that on our integration test, there are many assert_enqueued_jobs 1 code are commented out, then I ask other developer about it, He told me that we've changed many ActiveJob perform_later call to perform_now for speed reason, and that assert_enqueued_jobs can't catch those performed now jobs. 最近在我当前的项目中,我注意到在集成测试中,有很多assert_enqueued_jobs 1代码被注释掉了,然后我问其他开发者。他告诉我,出于速度原因,我们将许多ActiveJob perform_later调用更改为perform_now ,而assert_enqueued_jobs无法捕获那些现在执行的作业。

I've tried using assert_performed_jobs also but didn't worked out. 我也尝试过使用assert_performed_jobs但没有解决。

Can anyone give me insight or suggestion to test it? 谁能给我见识或建议进行测试? or it just can't be testable? 还是无法测试?

Untestable? 无法测试? Never! 决不!

The assert_enqueded_jobs is not actually testing code, rather checking that something was actually enqueued to happen later. assert_enqueded_jobs实际上不是在测试代码,而是在检查是否确实将某些内容排入队列以便稍后进行。 If something happens right away, why test that it was enqueued? 如果有事情马上发生,为什么还要测试它是否已入队?

I would try to keep the queue and force jobs to be performed/cleared using other of the ActiveJob::TestHelpers . 我会尝试保持队列并使用其他ActiveJob::TestHelpers强制执行/清除ActiveJob::TestHelpers But to that's just me, it makes little difference. 但这仅是我,没有什么区别。

https://apidock.com/rails/v4.2.1/ActiveJob/TestHelper/assert_enqueued_jobs https://apidock.com/rails/v4.2.1/ActiveJob/TestHelper/assert_enqueued_jobs

Say your job was to send some email, just call the ActiveJob#perform_now and check ActionMailer::Base.deliveries.count . 假设您的工作是发送一些电子邮件,只需致电ActiveJob#perform_now并检查ActionMailer::Base.deliveries.count At this point, the actual test will be very tailored to the job. 此时,实际测试将非常适合该工作。

Could be creating a Notification and you might want to assert that Notification.count has changed, whatever. 可能正在创建一个Notification并且您可能想要断言Notification.count已更改,无论如何。

The main thing is that instead of checking that the job was enqueued and end of story, we're looking for the desired outcome from that job running. 最主要的是,我们不是在检查工作是否已入队并结束故事,而是从该工作的运行中寻找所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM