Skip to content

Commit

Permalink
Tidied up Mock class. Don't delegate to Object#mocha_inspect for Mock…
Browse files Browse the repository at this point in the history
…#mocha_inspect so that we don't run into problems when mocking #inspect method. Remove __mock_name accessor which was only needed for testing.
  • Loading branch information
floehopper committed Jan 11, 2007
1 parent 4b32739 commit 3b3136e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
33 changes: 8 additions & 25 deletions lib/mocha/mock.rb
@@ -1,37 +1,20 @@
require 'mocha/mock_methods'

module Mocha

module ExtraMethods

attr_reader :__mock_name
class Mock

include MockMethods

def initialize(stub_everything = false, name = nil)
@stub_everything = stub_everything
@__mock_name = name
@mock_name = name
end

alias :mocha_inspect_before_hijacked_by_named_mocks :mocha_inspect

def mocha_inspect
@__mock_name ? "#<Mock:#{@__mock_name}>" : mocha_inspect_before_hijacked_by_named_mocks
@mock_name ? "#<Mock:#{@mock_name}>" : "#<Mock:0x#{__id__.to_s(16)}>"
end

end

class Mock

include MockMethods
include ExtraMethods

end

class BlankMock

methods_to_keep = /^__.*__$|respond_to?|mocha_inspect|inspect|class|object_id|send|is_a\?|==|hash|nil\?|extend|eql\?/
instance_methods.each { |method_to_remove| eval("undef :#{method_to_remove}") unless method_to_remove =~ methods_to_keep }

include MockMethods
include ExtraMethods

end

end
6 changes: 3 additions & 3 deletions test/mocha/auto_verify_test.rb
Expand Up @@ -147,17 +147,17 @@ def test_should_add_greedy_stub_to_mocks

def test_should_create_mock_with_name
mock = test_case.mock('named_mock')
assert_equal 'named_mock', mock.__mock_name
assert_equal '#<Mock:named_mock>', mock.mocha_inspect
end

def test_should_create_stub_with_name
stub = test_case.stub('named_stub')
assert_equal 'named_stub', stub.__mock_name
assert_equal '#<Mock:named_stub>', stub.mocha_inspect
end

def test_should_create_greedy_stub_with_name
greedy_stub = test_case.stub_everything('named_greedy_stub')
assert_equal 'named_greedy_stub', greedy_stub.__mock_name
assert_equal '#<Mock:named_greedy_stub>', greedy_stub.mocha_inspect
end

end
18 changes: 7 additions & 11 deletions test/mocha/mock_test.rb
Expand Up @@ -35,23 +35,19 @@ def test_should_stub_everything
assert_equal true, mock.stub_everything
end

def test_should_use_default_inspect_message
def test_should_display_object_id_for_inspect_if_mock_has_no_name
mock = Mock.new
assert_equal mock.mocha_inspect_before_hijacked_by_named_mocks, mock.mocha_inspect
assert_match Regexp.new("#<Mock:0x[0-9A-Fa-f]{6}>"), mock.mocha_inspect
end

def test_should_give_name_in_inspect_message
def test_should_display_name_for_inspect_if_mock_has_name
mock = Mock.new(false, 'named_mock')
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
end

def test_should_be_able_to_mock_some_standard_object_methods_on_blank_mock
mock = BlankMock.new
mock.expects(:type)
mock.expects(:kind_of?)
mock.type
mock.kind_of?
assert_nothing_raised(ExpectationError) { mock.verify }

def test_should_give_name_in_inspect_message
mock = Mock.new(false, 'named_mock')
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
end

def test_should_be_able_to_extend_mock_object_with_module
Expand Down

0 comments on commit 3b3136e

Please sign in to comment.