简体   繁体   中英

How to test Resque worker

Within my rails app, I have a worker that is reading an AWS SQS and then simply enqueues another worker onto Resque. I'm looking for the best way to test this with RSpec.

Is there a way to look to see what is on a mock Resque queue? Basically, I just want a test that tests whether calling the SQS worker results in a Resque worker.

Are you also using Resque to read from SQS? If so, you can use an around hook to enable Resque's inline mode. (This way Resque executes the jobs right away without actually queueing them.)

around do |example|
  Resque.inline = true
  example.run
  Resque.inline = false
end

Based on your code, you could then either check to see if the other worker has done its job (eg if it has created a record by checking the related records' count difference), or you could just use RSpec's have_received method to make sure the other worker has been queued by Resque.

  allow(Resque).to receive(:enqueue)
  SQSWorker.perform
  expect(Resque).to have_received(:enqueue).with(
    OtherWorker,
    optional: 'args'
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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