Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

ruby on rails - 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.

question from:https://stackoverflow.com/questions/65836203/how-to-test-resque-worker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 (e.g. 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'
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...