Skip to content
This repository

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

Merged
merged 4 commits into from 11 months ago

3 participants

Michi Huber Myron Marston Sam Phippen
Myron Marston

Thanks, @michihuber. I need to go back and read #133 to fully understand the discussion before merging this, but it looks generally good.

Myron Marston

Rather than adding a mock expectation to every example in this group (which would cause them all to fail if the warning is removed), I'd rather see something like:

before { RSpec::Mocks.stub(:warn) }

# the other examples

it 'prints a warning since this is really just a stub' do
  RSpec::Mocks.should_receive(:warn).with(/at_least\(0\) is deprecated/)
  @double.should_receive(:do_something).at_least(0).times
end
Michi Huber

Let me know if you also want to deprecate #any_number_of_times.

Sam Phippen
Collaborator

I think this looks fine now. We've fixed the problems with the specs and I'm generally in favour of this. @myronmarston what do you think?

Sam Phippen samphippen merged commit 37d9a71 into from
Sam Phippen samphippen closed this
Sam Phippen samphippen referenced this pull request from a commit
Sam Phippen Add a changelog entry for #231 and #237
Signed-off-by: Sam Phippen <samphippen@googlemail.com>
2efca50
Sam Phippen samphippen referenced this pull request
Closed

Don't allow at_least(0) #133

Yuji Nakayama yujinakayama referenced this pull request in yujinakayama/transpec
Closed

Support conversion of `at_least(0)` #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
6  lib/rspec/mocks/message_expectation.rb
@@ -302,6 +302,12 @@ def exactly(n, &block)
302 302
       #
303 303
       #   dealer.should_receive(:deal_card).at_least(9).times
304 304
       def at_least(n, &block)
  305
+        if n == 0
  306
+          RSpec::Mocks.warn_deprecation <<-MSG
  307
+DEPRECATION: at_least(0) is deprecated. Use #stub instead of #should_receive. Called from #{caller(0)[1]}
  308
+MSG
  309
+        end
  310
+
305 311
         @implementation = block if block
306 312
         set_expected_received_count :at_least, n
307 313
         self
59  spec/rspec/mocks/at_least_spec.rb
@@ -97,31 +97,40 @@ module Mocks
97 97
         @double.rspec_verify
98 98
       end
99 99
 
100  
-      it "passes with at_least(0) with no return if called once" do
101  
-        @double.should_receive(:do_something).at_least(0).times
102  
-        @double.do_something
103  
-      end
104  
-
105  
-      it "passes with at_least(0) with return block if called once" do
106  
-        @double.should_receive(:do_something).at_least(0).times { true }
107  
-        @double.do_something
108  
-      end
109  
-
110  
-      it "passes with at_least(0) with and_return if called once" do
111  
-        @double.should_receive(:do_something).at_least(0).times.and_return true
112  
-        @double.do_something
113  
-      end
114  
-
115  
-      it "passes with at_least(0) with no return if never called" do
116  
-        @double.should_receive(:do_something).at_least(0).times
117  
-      end
118  
-
119  
-      it "passes with at_least(0) with return block if never called" do
120  
-        @double.should_receive(:do_something).at_least(0).times { true }
121  
-      end
122  
-
123  
-      it "passes with at_least(0) with and_return if never called" do
124  
-        @double.should_receive(:do_something).at_least(0).times.and_return true
  100
+      context "when sent with 0" do
  101
+        before { RSpec::Mocks.stub(:warn_deprecation) }
  102
+
  103
+        it "outputs a deprecation warning" do
  104
+          RSpec::Mocks.should_receive(:warn_deprecation).with(/at_least\(0\) is deprecated/)
  105
+          @double.should_receive(:do_something).at_least(0).times
  106
+        end
  107
+
  108
+        it "passes with no return if called once" do
  109
+          @double.should_receive(:do_something).at_least(0).times
  110
+          @double.do_something
  111
+        end
  112
+
  113
+        it "passes with return block if called once" do
  114
+          @double.should_receive(:do_something).at_least(0).times { true }
  115
+          @double.do_something
  116
+        end
  117
+
  118
+        it "passes with and_return if called once" do
  119
+          @double.should_receive(:do_something).at_least(0).times.and_return true
  120
+          @double.do_something
  121
+        end
  122
+
  123
+        it "passes with no return if never called" do
  124
+          @double.should_receive(:do_something).at_least(0).times
  125
+        end
  126
+
  127
+        it "passes with return block if never called" do
  128
+          @double.should_receive(:do_something).at_least(0).times { true }
  129
+        end
  130
+
  131
+        it "passes with and_return if never called" do
  132
+          @double.should_receive(:do_something).at_least(0).times.and_return true
  133
+        end
125 134
       end
126 135
 
127 136
       it "uses a stub value if no value set" do
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.