Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit fcbbaba

Browse files
author
Sam Phippen
committed
Use syntax agnostic matchers for should syntax.
Signed-off-by: Sam Phippen <[email protected]>
1 parent 73a67bc commit fcbbaba

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

lib/rspec/mocks.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ def any_instance_recorder_for(klass)
3030
space.any_instance_recorder_for(klass)
3131
end
3232

33-
def allow_message(subject, message, &block)
33+
# Adds an allowance (stub) on `subject`
34+
def allow_message(subject, message, opts={}, &block)
3435
::RSpec::Mocks.proxy_for(subject).
35-
add_stub(caller(1)[0], message.to_sym, &block)
36+
add_stub(caller(1)[0], message.to_sym, opts, &block)
3637
end
3738

38-
def expect_message(subject, message, &block)
39+
# Sets a message expectation on `subject`.
40+
def expect_message(subject, message, opts={}, orig_caller=caller(1)[0], &block)
3941
::RSpec::Mocks.proxy_for(subject).
40-
add_message_expectation(caller(1)[0], message.to_sym, &block)
42+
add_message_expectation(opts[:expected_from] || orig_caller, message.to_sym, opts, &block)
4143
end
4244

4345
# @api private

lib/rspec/mocks/proxy_for_nil.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def add_stub(location, method_name, opts={}, &implementation)
2828
private
2929

3030
def warn method_name
31-
Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{caller[2]}. Use allow_message_expectations_on_nil to disable warnings.")
31+
Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{caller[3]}. Use allow_message_expectations_on_nil to disable warnings.")
3232
end
3333

3434
end

lib/rspec/mocks/syntax.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ def self.enable_should(syntax_host = default_should_syntax_host)
1212

1313
syntax_host.class_eval do
1414
def should_receive(message, opts={}, &block)
15-
proxy = ::RSpec::Mocks.proxy_for(self)
16-
proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block)
15+
::RSpec::Mocks.expect_message(self, message.to_sym, opts, caller(1)[0], &block)
1716
end
1817

1918
def should_not_receive(message, &block)
20-
proxy = ::RSpec::Mocks.proxy_for(self)
21-
proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block)
19+
::RSpec::Mocks.expect_message(self, message.to_sym, {}, caller(1)[0], &block).never
2220
end
2321

2422
def stub(message_or_hash, opts={}, &block)
2523
if ::Hash === message_or_hash
2624
message_or_hash.each {|message, value| stub(message).and_return value }
2725
else
28-
::RSpec::Mocks.proxy_for(self).add_stub(caller(1)[0], message_or_hash.to_sym, opts, &block)
26+
::RSpec::Mocks.allow_message(self, message_or_hash, opts, &block)
2927
end
3028
end
3129

spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ module Mocks
4646
expect { verify subject }.to raise_error(RSpec::Mocks::MockExpectationError)
4747
end
4848

49+
it "fails if never is specified and the message is called" do
50+
expect {
51+
::RSpec::Mocks.expect_message(subject, :foo).never
52+
subject.foo
53+
}.to raise_error(/expected.*0 times/)
54+
end
55+
4956
it "sets up basic message expectation, verifies as called" do
5057
::RSpec::Mocks.expect_message(subject, :basic)
5158
subject.basic

0 commit comments

Comments
 (0)