From 3b3136ecd40a224ba8083bf6872f8eed513c164f Mon Sep 17 00:00:00 2001 From: James Mead Date: Thu, 11 Jan 2007 16:15:08 +0000 Subject: [PATCH] Tidied up Mock class. Don't delegate to Object#mocha_inspect for Mock#mocha_inspect so that we don't run into problems when mocking #inspect method. Remove __mock_name accessor which was only needed for testing. --- lib/mocha/mock.rb | 33 ++++++++------------------------- test/mocha/auto_verify_test.rb | 6 +++--- test/mocha/mock_test.rb | 18 +++++++----------- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 5d9527cad..1924aa89c 100644 --- a/lib/mocha/mock.rb +++ b/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 ? "#" : mocha_inspect_before_hijacked_by_named_mocks + @mock_name ? "#" : "#" 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 \ No newline at end of file diff --git a/test/mocha/auto_verify_test.rb b/test/mocha/auto_verify_test.rb index f206db84c..93abc40ca 100644 --- a/test/mocha/auto_verify_test.rb +++ b/test/mocha/auto_verify_test.rb @@ -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.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 '#', 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 '#', greedy_stub.mocha_inspect end end \ No newline at end of file diff --git a/test/mocha/mock_test.rb b/test/mocha/mock_test.rb index bbb6ce6fb..9e4178435 100644 --- a/test/mocha/mock_test.rb +++ b/test/mocha/mock_test.rb @@ -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.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.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.mocha_inspect end def test_should_be_able_to_extend_mock_object_with_module