-
-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Externalize methods and state that was previously held on all objects. #250
Changes from 3 commits
73fb798
210c9f0
554d80c
d2cc42d
cccfb46
e57b5cc
34053de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ module Methods | |
# logger.should_receive(:log) | ||
# thing_that_logs.do_something_that_logs_a_message | ||
def should_receive(message, opts={}, &block) | ||
__mock_proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block) | ||
mock_proxy = ::RSpec::Mocks.space.mock_proxy_for(self) | ||
mock_proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block) | ||
end | ||
|
||
# Sets and expectation that this object should _not_ receive a message | ||
# during this example. | ||
def should_not_receive(message, &block) | ||
__mock_proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block) | ||
mock_proxy = ::RSpec::Mocks.space.mock_proxy_for(self) | ||
mock_proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block) | ||
end | ||
|
||
# Tells the object to respond to the message with the specified value. | ||
|
@@ -32,7 +34,7 @@ def stub(message_or_hash, opts={}, &block) | |
if Hash === message_or_hash | ||
message_or_hash.each {|message, value| stub(message).and_return value } | ||
else | ||
__mock_proxy.add_stub(caller(1)[0], message_or_hash.to_sym, opts, &block) | ||
::RSpec::Mocks.space.mock_proxy_for(self).add_stub(caller(1)[0], message_or_hash.to_sym, opts, &block) | ||
end | ||
end | ||
|
||
|
@@ -44,7 +46,7 @@ def stub(message_or_hash, opts={}, &block) | |
# shared `before` hook for the common case, but you want to replace it | ||
# for a special case. | ||
def unstub(message) | ||
__mock_proxy.remove_stub(message) | ||
::RSpec::Mocks.space.mock_proxy_for(self).remove_stub(message) | ||
end | ||
|
||
def stub!(message_or_hash, opts={}, &block) | ||
|
@@ -93,7 +95,7 @@ def stub_chain(*chain, &blk) | |
# returned. | ||
def as_null_object | ||
@_null_object = true | ||
__mock_proxy.as_null_object | ||
::RSpec::Mocks.space.mock_proxy_for(self).as_null_object | ||
end | ||
|
||
# Returns true if this object has received `as_null_object` | ||
|
@@ -103,37 +105,9 @@ def null_object? | |
|
||
# @private | ||
def received_message?(message, *args, &block) | ||
__mock_proxy.received_message?(message, *args, &block) | ||
end | ||
|
||
# @private | ||
def rspec_verify | ||
__mock_proxy.verify | ||
end | ||
|
||
# @private | ||
def rspec_reset | ||
__mock_proxy.reset | ||
end | ||
|
||
private | ||
|
||
def __mock_proxy | ||
@mock_proxy ||= begin | ||
mp = if TestDouble === self | ||
Proxy.new(self, @name, @options) | ||
else | ||
Proxy.new(self) | ||
end | ||
|
||
Serialization.fix_for(self) | ||
mp | ||
end | ||
end | ||
|
||
def __remove_mock_proxy | ||
@mock_proxy = nil | ||
::RSpec::Mocks.space.mock_proxy_for(self).received_message?(message, *args, &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used ::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By "attach" I mean "redirect to" i.e. it would still be implemented on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how that reads. |
||
end | ||
end | ||
end | ||
end | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,32 +2,50 @@ module RSpec | |
module Mocks | ||
# @api private | ||
class Space | ||
def add(obj) | ||
receivers << obj unless receivers.detect {|m| m.equal? obj} | ||
attr_reader :mock_proxies | ||
|
||
def initialize | ||
@mock_proxies = {} | ||
end | ||
|
||
def verify_all | ||
receivers.each do |mock| | ||
mock.rspec_verify | ||
mock_proxies.values.each do |object| | ||
object.verify | ||
end | ||
|
||
AnyInstance.verify_all | ||
end | ||
|
||
def reset_all | ||
receivers.each do |mock| | ||
mock.rspec_reset | ||
ConstantMutator.reset_all | ||
AnyInstance.reset_all | ||
|
||
mock_proxies.values.each do |object| | ||
object.reset | ||
end | ||
receivers.clear | ||
|
||
mock_proxies.clear | ||
expectation_ordering.clear | ||
end | ||
|
||
def expectation_ordering | ||
@expectation_ordering ||= OrderGroup.new | ||
end | ||
|
||
private | ||
def mock_proxy_for(object) | ||
mock_proxies.fetch(object.object_id) do | ||
mock_proxies[object.object_id] = if TestDouble === object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this hard to read. I'd either indent the lines below so they are aligned with the
|
||
object.__build_mock_proxy | ||
else | ||
Proxy.new(object) | ||
end | ||
end | ||
end | ||
|
||
alias ensure_registered mock_proxy_for | ||
|
||
def receivers | ||
@receivers ||= [] | ||
def registered?(object) | ||
mock_proxies.has_key?(object.object_id) | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just
tracked_classes
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No particular reason; I'm just in the habit of using
klass
in the place ofclass
sinceclass
is a keyword. Here as part of a longer variable nametracked_classes
is fine. I'll update it.