Pruebas de escritura para un trabajador sidekiq
Frecuentes
Visto 33,134 veces
23
Estoy usando el rspec-sidekiq
joyahttps://github.com/philostler/rspec-sidekiq) to help test a worker I am writing, but for some reason my test keeps failing.
Aquí está mi prueba:
require 'spec_helper'
describe CommunicationWorker do
it { should be_retryable false }
it "enqueues a communication worker" do
subject.perform("foo@bar.com", "bar@foo.com", [1,2,3])
expect(CommunicationWorker).to have_enqueued_jobs(1)
end
end
Aquí está el error:
1) CommunicationWorker enqueues a communication worker
Failure/Error: expect(CommunicationWorker).to have_enqueued_jobs(1)
expected CommunicationWorker to have 1 enqueued job but got 0
# ./spec/workers/communication_worker_spec.rb:9:in `block (2 levels) in <top (required)>'
I based my low-level test off of their example on their wiki, but it's not working for me... any reason why this wouldn't work?
2 Respuestas
32
There are two things to test for here, the asynchronous enqueuing of the job in the queue and the execution of the job.
You can test the execution of the job by instantiating the job class and calling perform()
.
You can test the enqueuing of the job by calling perform_async()
on the job class.
To test the expectation in your test, you should be doing:
it "enqueues a communication worker" do
CommunicationWorker.perform_async("foo@bar.com", "bar@foo.com", [1,2,3])
expect(CommunicationWorker).to have(1).jobs
end
However, this is really just testing the Sidekiq framework and not a useful test. I would recommend writing tests for the internal behavior of the job itself:
it "enqueues a communication worker" do
Widget.expects(:do_work).with(:some_value)
Mailer.expects(:deliver)
CommunicationWorker.new.perform("foo@bar.com", "bar@foo.com", [1,2,3])
end
respondido 19 mar '14, 17:03
At the time this answer was given, I'm guessing have_enqueued_jobs
was still valid but now the proper syntax is have(1).jobs
- harasho
thank you! and for google: has_enqueued_job? I found precisely zero results for that string - Michael
For rspec 3, this will work: expect(CommunicationWorker.jobs.size).to eq(1)
- mikkeljuhl
2
What is the testing method? Try wrapping your existing test with Sidekiq::Testing.fake! do <your code> end
. This will ensure the a fake queue is used. If sidekiq's testing methods is 'inline', the worker will be executed right away (and thus your queue will be 0 length).
Eche un vistazo a: https://github.com/mperham/sidekiq/wiki/Testing para más información.
Respondido el 16 de diciembre de 13 a las 13:12
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas rspec rspec-rails sidekiq rspec-sidekiq or haz tu propia pregunta.
Did you ever get this working? I'm running into the same issue. - hiattp