From fb996b7d31786621dddbd300f1d5acee3125bfee Mon Sep 17 00:00:00 2001 From: Povilas Jurcys Date: Wed, 13 Sep 2017 16:21:33 +0300 Subject: [PATCH 1/2] Make sure that negative matcher fails on multiple enqueues of the same kind --- lib/rspec/rails/matchers/active_job.rb | 37 ++++++++++++++++++-- spec/rspec/rails/matchers/active_job_spec.rb | 18 +++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/rspec/rails/matchers/active_job.rb b/lib/rspec/rails/matchers/active_job.rb index 7e9492299d..d8150a17c0 100644 --- a/lib/rspec/rails/matchers/active_job.rb +++ b/lib/rspec/rails/matchers/active_job.rb @@ -16,7 +16,8 @@ def initialize @queue = nil @at = nil @block = Proc.new {} - set_expected_number(:exactly, 1) + @using_default_expected_number = true + silently_set_expected_number(:exactly, 1) end def with(*args, &block) @@ -78,7 +79,7 @@ def failure_message end def failure_message_when_negated - "expected not to enqueue #{base_message}" + "expected not to enqueue #{base_negative_message}" end def message_expectation_modifier @@ -95,6 +96,10 @@ def supports_block_expectations? private + def using_default_expected_number? + @using_default_expected_number + end + def check(jobs) @matching_jobs, @unmatching_jobs = jobs.partition do |job| if arguments_match?(job) && other_attributes_match?(job) @@ -115,7 +120,19 @@ def check(jobs) end def base_message - "#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg| + message_with_base_info("#{message_expectation_modifier} #{@expected_number} jobs,") + end + + def base_negative_message + if @expectation_type == :exactly && @expected_number.zero? + message_with_base_info("job,") + else + base_message + end + end + + def message_with_base_info(intro_message) + intro_message.dup.tap do |msg| msg << " with #{@args}," if @args.any? msg << " on queue #{@queue}," if @queue msg << " at #{@at}," if @at @@ -156,6 +173,11 @@ def serialized_attributes end def set_expected_number(relativity, count) + @using_default_expected_number = false + silently_set_expected_number(relativity, count) + end + + def silently_set_expected_number(relativity, count) @expectation_type = relativity @expected_number = case count when :once then 1 @@ -187,6 +209,15 @@ def matches?(proc) check(in_block_jobs) end + + def does_not_match?(proc) + if using_default_expected_number? + silently_set_expected_number(:exactly, 0) + matches?(proc) + else + !matches?(proc) + end + end end # @private diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index e9255add1e..9a61c32aa2 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -116,7 +116,7 @@ def self.name; "LoggingJob"; end it "fails when negated and job is enqueued" do expect { expect { heavy_lifting_job.perform_later }.not_to have_enqueued_job - }.to raise_error(/expected not to enqueue exactly 1 jobs, but enqueued 1/) + }.to raise_error(/expected not to enqueue job, but enqueued 1/) end it "passes with job name" do @@ -134,6 +134,22 @@ def self.name; "LoggingJob"; end }.to have_enqueued_job(hello_job).and have_enqueued_job(logging_job) end + it "passes with multiple jobs of the same kind and different expected count when negated" do + expect { + hello_job.perform_later + hello_job.perform_later + }.not_to have_enqueued_job.once + end + + it "fails with multiple same type jobs when negated" do + expect { + expect { + hello_job.perform_later + hello_job.perform_later + }.not_to have_enqueued_job + }.to raise_error(/expected not to enqueue job, but enqueued 2/) + end + it "passes with :once count" do expect { hello_job.perform_later From 38801acd0c0bd00af29d9d22eae7c88251ccc083 Mon Sep 17 00:00:00 2001 From: Povilas Jurcys Date: Fri, 22 Sep 2017 08:26:37 +0300 Subject: [PATCH 2/2] Include more negotate test examples --- spec/rspec/rails/matchers/active_job_spec.rb | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 9a61c32aa2..c0bd85aeba 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -106,6 +106,34 @@ def self.name; "LoggingJob"; end }.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 2/) end + + it "shows correct fail message when negated with at_least matcher" do + expect { + expect { + heavy_lifting_job.perform_later + heavy_lifting_job.perform_later + }.not_to have_enqueued_job.at_least(2) + }.to raise_error(/expected not to enqueue at least 2 jobs, but enqueued 2/) + end + + it "shows correct fail message when negated with at_most matcher" do + expect { + expect { + heavy_lifting_job.perform_later + heavy_lifting_job.perform_later + }.not_to have_enqueued_job.at_most(2) + }.to raise_error(/expected not to enqueue at most 2 jobs, but enqueued 2/) + end + + it "fails when too many jobs enqueued in negated form" do + expect { + expect { + heavy_lifting_job.perform_later + heavy_lifting_job.perform_later + }.not_to have_enqueued_job.exactly(2) + }.to raise_error(/expected not to enqueue exactly 2 jobs, but enqueued 2/) + end + it "reports correct number in fail error message" do heavy_lifting_job.perform_later expect {