diff --git a/core/app/jobs/spree/promotion_code_batch_job.rb b/core/app/jobs/spree/promotion_code_batch_job.rb index 9fc8c7c1991..2fbeaf48774 100644 --- a/core/app/jobs/spree/promotion_code_batch_job.rb +++ b/core/app/jobs/spree/promotion_code_batch_job.rb @@ -7,13 +7,17 @@ def perform(promotion_code_batch) promotion_code_batch ).build_promotion_codes - Spree::PromotionCodeBatchMailer - .promotion_code_batch_finished(promotion_code_batch) - .deliver_now + if promotion_code_batch.email? + Spree::PromotionCodeBatchMailer + .promotion_code_batch_finished(promotion_code_batch) + .deliver_now + end rescue => e - Spree::PromotionCodeBatchMailer - .promotion_code_batch_errored(promotion_code_batch) - .deliver_now + if promotion_code_batch.email? + Spree::PromotionCodeBatchMailer + .promotion_code_batch_errored(promotion_code_batch) + .deliver_now + end raise e end end diff --git a/core/spec/jobs/promotion_code_batch_job_spec.rb b/core/spec/jobs/promotion_code_batch_job_spec.rb index d206dc4c337..01dff358973 100644 --- a/core/spec/jobs/promotion_code_batch_job_spec.rb +++ b/core/spec/jobs/promotion_code_batch_job_spec.rb @@ -1,11 +1,12 @@ require 'spec_helper' describe Spree::PromotionCodeBatchJob, type: :job do + let(:email) { "test@email.com" } let(:promotion_code_batch) do Spree::PromotionCodeBatch.create!( promotion_id: create(:promotion).id, base_code: "test", number_of_codes: 10, - email: "test@email.com" + email: email ) end @@ -15,10 +16,20 @@ .to receive(:promotion_code_batch_finished) .and_call_original end - it "sends an email" do - subject.perform(promotion_code_batch) - expect(Spree::PromotionCodeBatchMailer) - .to have_received(:promotion_code_batch_finished) + context "with an email address" do + it "sends an email" do + subject.perform(promotion_code_batch) + expect(Spree::PromotionCodeBatchMailer) + .to have_received(:promotion_code_batch_finished) + end + end + context "with no email address" do + let(:email) { nil } + it "sends an email" do + subject.perform(promotion_code_batch) + expect(Spree::PromotionCodeBatchMailer) + .to_not have_received(:promotion_code_batch_finished) + end end end @@ -36,9 +47,19 @@ .to raise_error RuntimeError end - it "sends an email" do - expect(Spree::PromotionCodeBatchMailer) - .to have_received(:promotion_code_batch_errored) + context "with an email address" do + it "sends an email" do + expect(Spree::PromotionCodeBatchMailer) + .to have_received(:promotion_code_batch_errored) + end + end + + context "with no email address" do + let(:email) { nil } + it "sends an email" do + expect(Spree::PromotionCodeBatchMailer) + .to_not have_received(:promotion_code_batch_errored) + end end end end