From 9d8e81d217b8d728aff745da48e9401651cca572 Mon Sep 17 00:00:00 2001 From: Neha Kumari Date: Fri, 8 Jun 2012 13:57:18 +0530 Subject: [PATCH] Issue #133: Disallow at_least(0). --- lib/rspec/mocks/error_generator.rb | 5 +++++ lib/rspec/mocks/message_expectation.rb | 19 ++++++++++++++++- spec/rspec/mocks/at_least_spec.rb | 28 +++----------------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/rspec/mocks/error_generator.rb b/lib/rspec/mocks/error_generator.rb index e7ba3cbcc..983beadef 100644 --- a/lib/rspec/mocks/error_generator.rb +++ b/lib/rspec/mocks/error_generator.rb @@ -66,6 +66,11 @@ def raise_wrong_arity_error(args_to_yield, arity) __raise "#{intro} yielded |#{arg_list(*args_to_yield)}| to block with arity of #{arity}" end + # @private + def raise_wrong_expected_receive_count_error(message) + __raise "#{intro} received :#{message} with invalid expectation\n expected: at_least(n) with n > 0 \n got: 0" + end + private def intro diff --git a/lib/rspec/mocks/message_expectation.rb b/lib/rspec/mocks/message_expectation.rb index 89d64aaca..9a73272c9 100644 --- a/lib/rspec/mocks/message_expectation.rb +++ b/lib/rspec/mocks/message_expectation.rb @@ -233,6 +233,11 @@ def matches_at_least_count? @at_least && @actual_received_count >= @expected_received_count end + # @private + def at_least_count_greater_than_zero?(n) + @at_least ? n > 0 : true + end + # @private def matches_at_most_count? @at_most && @actual_received_count <= @expected_received_count @@ -266,6 +271,10 @@ def raise_out_of_order_error @error_generator.raise_out_of_order_error @message end + def raise_wrong_expected_receive_count_error + @error_generator.raise_wrong_expected_receive_count_error(@message) + end + # Constrains a stub or message expectation to invocations with specific # arguments. # @@ -449,8 +458,15 @@ def set_expected_received_count(relativity, n) @at_least = (relativity == :at_least) @at_most = (relativity == :at_most) @exactly = (relativity == :exactly) + @expected_received_count = case n - when Numeric then n + when Numeric + if at_least_count_greater_than_zero?(n) + n + else + @expected_received_count = 0 + raise_wrong_expected_receive_count_error + end when :once then 1 when :twice then 2 end @@ -466,6 +482,7 @@ def build_implementation(values) value = values.size == 1 ? values.first : values lambda { value } end + end # @private diff --git a/spec/rspec/mocks/at_least_spec.rb b/spec/rspec/mocks/at_least_spec.rb index 54f6a5664..459e85d4c 100644 --- a/spec/rspec/mocks/at_least_spec.rb +++ b/spec/rspec/mocks/at_least_spec.rb @@ -97,33 +97,11 @@ module Mocks @double.rspec_verify end - it "passes with at_least(0) with no return if called once" do - @double.should_receive(:do_something).at_least(0).times - @double.do_something - end - it "passes with at_least(0) with return block if called once" do - @double.should_receive(:do_something).at_least(0).times { true } - @double.do_something + it "raises wrong expected receive count error when the at least 0 method is called" do + expect{ @double.should_receive(:do_something).at_least(0) }.to raise_error(RSpec::Mocks::MockExpectationError, /expected: at_least\(n\) with n > 0/) end - - it "passes with at_least(0) with and_return if called once" do - @double.should_receive(:do_something).at_least(0).times.and_return true - @double.do_something - end - - it "passes with at_least(0) with no return if never called" do - @double.should_receive(:do_something).at_least(0).times - end - - it "passes with at_least(0) with return block if never called" do - @double.should_receive(:do_something).at_least(0).times { true } - end - - it "passes with at_least(0) with and_return if never called" do - @double.should_receive(:do_something).at_least(0).times.and_return true - end - + it "uses a stub value if no value set" do @double.stub(:do_something => 'foo') @double.should_receive(:do_something).at_least(:once)