Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #133: Disallow at_least(0). #150

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/rspec/mocks/error_generator.rb
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
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
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