Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

warn about do/end blocks

  • Loading branch information...
commit 8db5cbea5f719d25540ea56aaaf9c170b13640f3 1 parent 2586e72
@JonRowe JonRowe authored
View
12 lib/rspec/mocks/matchers/receive_messages.rb
@@ -12,14 +12,15 @@ def name
"receive_messages"
end
- def setup_expectation(subject, &block)
+ def setup_expectation(subject)
+ warn_about_block if block_given?
each_message_on( proxy_on(subject) ) do |host, message, return_value|
host.add_simple_expectation(message, return_value, @backtrace_line)
end
end
alias matches? setup_expectation
- def setup_negative_expectation(subject, &block)
+ def setup_negative_expectation(subject)
raise NegationUnsupportedError,
"`expect(...).to_not receive_messages` is not supported since it " +
"doesn't really make sense. What would it even mean?"
@@ -27,21 +28,28 @@ def setup_negative_expectation(subject, &block)
alias does_not_match? setup_negative_expectation
def setup_allowance(subject)
+ warn_about_block if block_given?
each_message_on( proxy_on(subject) ) do |host, message, return_value|
host.add_simple_stub(message, return_value)
end
end
def setup_any_instance_expectation(subject)
+ warn_about_block if block_given?
each_message_on( any_instance_of(subject) ) do |host, message, return_value|
host.should_receive(message).and_return(return_value)
end
end
def setup_any_instance_allowance(subject)
+ warn_about_block if block_given?
any_instance_of(subject).stub(@message_return_value_hash)
end
+ def warn_about_block
+ raise "Implementation blocks aren't supported with `receive_messages`"
+ end
+
private
def proxy_on(subject)
View
5 lib/rspec/mocks/syntax.rb
@@ -93,8 +93,9 @@ def receive(method_name, &block)
end
def receive_messages(message_return_value_hash)
- raise "Implementation blocks aren't supported with `receive_messages`" if block_given?
- Matchers::ReceiveMessages.new(message_return_value_hash)
+ matcher = Matchers::ReceiveMessages.new(message_return_value_hash)
+ matcher.warn_about_block if block_given?
+ matcher
end
def allow(target)
View
40 spec/rspec/mocks/matchers/receive_messages_spec.rb
@@ -2,6 +2,22 @@
module RSpec
module Mocks
+ shared_examples_for "complains when given blocks" do
+ it "complains if a { } block is given" do
+ expect {
+ target.to receive_messages(:a => 1) { "implementation" }
+ }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ end
+
+ it "complains if a do; end; block is given" do
+ expect {
+ target.to receive_messages(:a => 1) do
+ "implementation"
+ end
+ }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ end
+ end
+
describe "allow(...).to receive_messages(:a => 1, :b => 2)" do
let(:obj) { double "Object" }
@@ -11,10 +27,8 @@ module Mocks
expect(obj.b).to eq 2
end
- it "complains if a block is given" do
- expect {
- allow(obj).to receive_messages(:a => 1) { "implementation" }
- }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ it_behaves_like "complains when given blocks" do
+ let(:target) { allow(obj) }
end
end
@@ -27,10 +41,8 @@ module Mocks
expect(obj.b).to eq 2
end
- it "complains if a block is given" do
- expect {
- allow_any_instance_of(Object).to receive_messages(:a => 1) { "implementation" }
- }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ it_behaves_like "complains when given blocks" do
+ let(:target) { allow_any_instance_of(Object) }
end
end
@@ -64,10 +76,8 @@ module Mocks
expect(expectation_error.backtrace[0]).to match /#{__FILE__}:#{line}/
end
- it "complains if a block is given" do
- expect {
- expect(double).to receive_messages(:a => 1) { "implementation" }
- }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ it_behaves_like "complains when given blocks" do
+ let(:target) { expect(double) }
end
end
@@ -80,10 +90,8 @@ module Mocks
expect { RSpec::Mocks.space.verify_all }.to raise_error RSpec::Mocks::MockExpectationError
end
- it "complains if a block is given" do
- expect {
- expect_any_instance_of(Object).to receive_messages(:a => 1) { "implementation" }
- }.to raise_error "Implementation blocks aren't supported with `receive_messages`"
+ it_behaves_like "complains when given blocks" do
+ let(:target) { expect_any_instance_of(Object) }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.