Skip to content

Commit

Permalink
Make mock cleanup simpler and saner.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Jul 8, 2009
1 parent e812953 commit fe6cb0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/mspec/mocks/mock.rb
@@ -1,6 +1,10 @@
require 'mspec/expectations/expectations'
require 'mspec/helpers/metaclass'

class Object
alias_method :__mspec_object_id__, :object_id
end

module Mock
def self.reset
@mocks = @stubs = @objects = nil
Expand All @@ -19,7 +23,7 @@ def self.stubs
end

def self.replaced_name(obj, sym)
:"__mspec_#{obj.__id__}_#{sym}__"
:"__mspec_#{obj.__mspec_object_id__}_#{sym}__"
end

def self.replaced_key(obj, sym)
Expand Down Expand Up @@ -166,7 +170,7 @@ def self.cleanup
sym = key.last
meta = obj.metaclass

if meta.instance_methods.map { |x| x.to_sym }.include?(replaced)
if mock_respond_to? obj, replaced
meta.__send__ :alias_method, sym, replaced
meta.__send__ :remove_method, replaced
else
Expand Down
4 changes: 2 additions & 2 deletions spec/mocks/mock_spec.rb
Expand Up @@ -22,15 +22,15 @@
describe Mock, ".replaced_name" do
it "returns the name for a method that is being replaced by a mock method" do
m = mock('a fake id')
m.stub!(:__id__).and_return(42)
m.stub!(:__mspec_object_id__).and_return(42)
Mock.replaced_name(m, :method_call).should == :__mspec_42_method_call__
end
end

describe Mock, ".replaced_key" do
it "returns a key used internally by Mock" do
m = mock('a fake id')
m.stub!(:__id__).and_return(42)
m.stub!(:__mspec_object_id__).and_return(42)
Mock.replaced_key(m, :method_call).should == [:__mspec_42_method_call__, :method_call]
end
end
Expand Down

0 comments on commit fe6cb0a

Please sign in to comment.