Permalink
Browse files

Issue #133: Disallow at_least(0).

  • Loading branch information...
1 parent 49ce496 commit 9d8e81d217b8d728aff745da48e9401651cca572 Neha Kumari committed Jun 8, 2012
Showing with 26 additions and 26 deletions.
  1. +5 −0 lib/rspec/mocks/error_generator.rb
  2. +18 −1 lib/rspec/mocks/message_expectation.rb
  3. +3 −25 spec/rspec/mocks/at_least_spec.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
@@ -234,6 +234,11 @@ def matches_at_least_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
end
@@ -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
@@ -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)

0 comments on commit 9d8e81d

Please sign in to comment.