Skip to content

Commit

Permalink
Issue rspec#133: Disallow at_least(0).
Browse files Browse the repository at this point in the history
  • Loading branch information
Neha Kumari committed Jun 8, 2012
1 parent 49ce496 commit 9d8e81d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
5 changes: 5 additions & 0 deletions lib/rspec/mocks/error_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion lib/rspec/mocks/message_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
#
Expand Down Expand Up @@ -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
Expand All @@ -466,6 +482,7 @@ def build_implementation(values)
value = values.size == 1 ? values.first : values
lambda { value }
end

end

# @private
Expand Down
28 changes: 3 additions & 25 deletions spec/rspec/mocks/at_least_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9d8e81d

Please sign in to comment.