Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

3 participants

Neha Don't Add Me To Your Organization a.k.a The Travis Bot David Chelimsky
Neha

Naive implementation for disallowing 0 with at_least.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 9d8e81d into 49ce496).

David Chelimsky
Owner

Per the convo on #132, we're not going to disallow this, but add a deprecation warning instead. I'd welcome a pull for that, if you're interested.

David Chelimsky dchelimsky closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 8, 2012
  1. Issue #133: Disallow at_least(0).

    Neha Kumari authored
This page is out of date. Refresh to see the latest.
5 lib/rspec/mocks/error_generator.rb
View
@@ -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
19 lib/rspec/mocks/message_expectation.rb
View
@@ -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
28 spec/rspec/mocks/at_least_spec.rb
View
@@ -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)
Something went wrong with that request. Please try again.