Skip to content

Commit

Permalink
Fixed bug that didn't clean up stubbed class methods. Also changed co…
Browse files Browse the repository at this point in the history
…pied code to a DRYer alias chain.
  • Loading branch information
Daniela Wellisz and Lars Petrus committed Feb 20, 2013
1 parent c87c2b9 commit e85f2f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
27 changes: 11 additions & 16 deletions lib/rspec-spies.rb
@@ -1,22 +1,17 @@
require 'rspec/mocks/method_double'
RSpec::Mocks::MethodDouble.class_eval do
# override defining stubs to record the message was called.
# there's only one line difference from upstream rspec, but can't change it without fully overriding
def define_proxy_method
return if @method_is_proxied
require 'rspec/mocks/proxy'

object_singleton_class.class_eval <<-EOF, __FILE__, __LINE__ + 1
def #{@method_name}(*args, &block)
__mock_proxy.record_message_received :#{@method_name}, *args, &block
__mock_proxy.message_received :#{@method_name}, *args, &block
end
#{visibility_for_method}
EOF
@method_is_proxied = true
RSpec::Mocks::Proxy.class_eval do
alias_method :oldsp_message_received, :message_received
alias_method :oldsp_reset, :reset

def message_received(message, *args, &block)
record_message_received(message, *args, &block)
oldsp_message_received(message, *args, &block)
end

def visibility_for_method
"#{visibility} :#{method_name}"
def reset
@messages_received = []
oldsp_reset
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/rspec-spies_spec.rb
Expand Up @@ -79,5 +79,21 @@ module Matchers
end
end

describe 'clearing out received messages' do
class Foo; end

before do
Foo.stub(:party)
end

it 'base case' do
Foo.party
have_received(:party).matches?(Foo).should be_true
end

it 'does not match even if the class method has been called in another spec' do
have_received(:party).matches?(Foo).should be_false
end
end
end
end

0 comments on commit e85f2f7

Please sign in to comment.