Skip to content

Output deprecation warning when at_least(0) is sent #231

Merged
merged 4 commits into from May 12, 2013
View
6 lib/rspec/mocks/message_expectation.rb
@@ -302,6 +302,12 @@ def exactly(n, &block)
#
# dealer.should_receive(:deal_card).at_least(9).times
def at_least(n, &block)
+ if n == 0
+ RSpec::Mocks.warn_deprecation <<-MSG
+DEPRECATION: at_least(0) is deprecated. Use #stub instead of #should_receive. Called from #{caller(0)[1]}
+MSG
+ end
+
@implementation = block if block
set_expected_received_count :at_least, n
self
View
59 spec/rspec/mocks/at_least_spec.rb
@@ -97,31 +97,40 @@ 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
- 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
+ context "when sent with 0" do
+ before { RSpec::Mocks.stub(:warn_deprecation) }
+
+ it "outputs a deprecation warning" do
+ RSpec::Mocks.should_receive(:warn_deprecation).with(/at_least\(0\) is deprecated/)
+ @double.should_receive(:do_something).at_least(0).times
+ end
+
+ it "passes with no return if called once" do
+ @double.should_receive(:do_something).at_least(0).times
+ @double.do_something
+ end
+
+ it "passes with return block if called once" do
+ @double.should_receive(:do_something).at_least(0).times { true }
+ @double.do_something
+ end
+
+ it "passes 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 no return if never called" do
+ @double.should_receive(:do_something).at_least(0).times
+ end
+
+ it "passes with return block if never called" do
+ @double.should_receive(:do_something).at_least(0).times { true }
+ end
+
+ it "passes with and_return if never called" do
+ @double.should_receive(:do_something).at_least(0).times.and_return true
+ end
end
it "uses a stub value if no value set" do
Something went wrong with that request. Please try again.