From 824c3420e9047aea760112ecbeb9c715cb74a574 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 9 Jan 2019 09:51:11 +1100 Subject: [PATCH] DEV: make Jobs.enqueue tests less fragile Previously we depended on non Sidekiq specific mocking which is not the official way of testing Sidekiq, this made these tests very fragile New testing is more robust and complete --- spec/jobs/jobs_spec.rb | 54 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/spec/jobs/jobs_spec.rb b/spec/jobs/jobs_spec.rb index 01cbe6a225f..a058971ed1d 100644 --- a/spec/jobs/jobs_spec.rb +++ b/spec/jobs/jobs_spec.rb @@ -11,13 +11,40 @@ describe Jobs do end it 'enqueues a job in sidekiq' do - Sidekiq::Client.expects(:enqueue).with(Jobs::ProcessPost, post_id: 1, current_site_id: 'default') - Jobs.enqueue(:process_post, post_id: 1) + Sidekiq::Testing.fake! do + jobs = Jobs::ProcessPost.jobs + + jobs.clear + Jobs.enqueue(:process_post, post_id: 1) + expect(jobs.length).to eq(1) + job = jobs.first + + expected = { + "class" => "Jobs::ProcessPost", + "args" => [{ "post_id" => 1, "current_site_id" => "default" }], + "queue" => "default" + } + expect(job.slice("class", "args", "queue")).to eq(expected) + end end it "does not pass current_site_id when 'all_sites' is present" do - Sidekiq::Client.expects(:enqueue).with(Jobs::ProcessPost, post_id: 1) - Jobs.enqueue(:process_post, post_id: 1, all_sites: true) + Sidekiq::Testing.fake! do + jobs = Jobs::ProcessPost.jobs + + jobs.clear + Jobs.enqueue(:process_post, post_id: 1, all_sites: true) + + expect(jobs.length).to eq(1) + job = jobs.first + + expected = { + "class" => "Jobs::ProcessPost", + "args" => [{ "post_id" => 1 }], + "queue" => "default" + } + expect(job.slice("class", "args", "queue")).to eq(expected) + end end it "doesn't execute the job" do @@ -27,10 +54,23 @@ describe Jobs do end it "should enqueue with the correct database id when the current_site_id option is given" do - Sidekiq::Client.expects(:enqueue).with do |arg1, arg2| - arg2[:current_site_id] == 'test_db' && arg2[:sync_exec].nil? + + Sidekiq::Testing.fake! do + jobs = Jobs::ProcessPost.jobs + + jobs.clear + Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db') + + expect(jobs.length).to eq(1) + job = jobs.first + + expected = { + "class" => "Jobs::ProcessPost", + "args" => [{ "post_id" => 1, "current_site_id" => "test_db" }], + "queue" => "default" + } + expect(job.slice("class", "args", "queue")).to eq(expected) end - Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db') end end