test/unit/mock_test.rb in mocha-1.6.0 vs test/unit/mock_test.rb in mocha-1.7.0

- old
+ new

@@ -4,26 +4,25 @@ require 'mocha/expectation_error_factory' require 'set' require 'simple_counter' class MockTest < Mocha::TestCase - include Mocha def test_should_set_single_expectation - mock = build_mock - mock.expects(:method1).returns(1) - assert_nothing_raised(ExpectationErrorFactory.exception_class) do - assert_equal 1, mock.method1 - end + mock = build_mock + mock.expects(:method1).returns(1) + assert_nothing_raised(ExpectationErrorFactory.exception_class) do + assert_equal 1, mock.method1 + end end def test_should_build_and_store_expectations - mock = build_mock - expectation = mock.expects(:method1) - assert_not_nil expectation - assert_equal [expectation], mock.__expectations__.to_a + mock = build_mock + expectation = mock.expects(:method1) + assert_not_nil expectation + assert_equal [expectation], mock.__expectations__.to_a end def test_should_not_stub_everything_by_default mock = build_mock assert_equal false, mock.everything_stubbed @@ -43,14 +42,28 @@ def test_should_be_equal mock = build_mock assert_equal true, mock.eql?(mock) end - if PRE_RUBY_V19 - OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || ["method_missing", "singleton_method_undefined", "initialize"].include?(m)} - else - OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || [:object_id, :method_missing, :singleton_method_undefined, :initialize, :String, :singleton_method_added].include?(m) } + EXCLUDED_METHODS = { + true => %w[ + method_missing + singleton_method_undefined + initialize + ], + false => [ + :object_id, + :method_missing, + :singleton_method_undefined, + :initialize, + :String, + :singleton_method_added + ] + }.freeze + + OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject do |m| + m =~ /^__.*__$/ || EXCLUDED_METHODS[PRE_RUBY_V19].include?(m) end def test_should_be_able_to_mock_standard_object_methods mock = build_mock OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) } @@ -147,11 +160,11 @@ def test_should_yield_supplied_parameters_to_block mock = build_mock parameters_for_yield = [1, 2, 3] mock.expects(:method1).yields(*parameters_for_yield) yielded_parameters = nil - mock.method1() { |*parameters| yielded_parameters = parameters } + mock.method1 { |*parameters| yielded_parameters = parameters } assert_equal parameters_for_yield, yielded_parameters end def test_should_set_up_multiple_expectations_with_return_values mock = build_mock @@ -226,38 +239,38 @@ assert_equal false, mock.respond_to?(:method1) end def test_should_respond_to_methods_which_the_responder_does_responds_to instance = Class.new do - define_method(:respond_to?) { |symbol| true } + define_method(:respond_to?) { |_symbol| true } end.new mock = build_mock mock.responds_like(instance) assert_equal true, mock.respond_to?(:invoked_method) end def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to instance = Class.new do - define_method(:respond_to?) { |symbol| false } + define_method(:respond_to?) { |_symbol| false } end.new mock = build_mock mock.responds_like(instance) assert_equal false, mock.respond_to?(:invoked_method) end def test_should_respond_to_methods_which_the_responder_instance_does_responds_to klass = Class.new do - define_method(:respond_to?) { |symbol| true } + define_method(:respond_to?) { |_symbol| true } end mock = build_mock mock.responds_like_instance_of(klass) assert_equal true, mock.respond_to?(:invoked_method) end def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to klass = Class.new do - define_method(:respond_to?) { |symbol| false } + define_method(:respond_to?) { |_symbol| false } end mock = build_mock mock.responds_like_instance_of(klass) assert_equal false, mock.respond_to?(:invoked_method) end @@ -278,31 +291,31 @@ assert_nothing_raised(NoMethodError) { mock.invoked_method } end def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method instance = Class.new do - define_method(:respond_to?) { |symbol| true } + define_method(:respond_to?) { |_symbol| true } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method) assert_nothing_raised(NoMethodError) { mock.invoked_method } end def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method instance = Class.new do - define_method(:respond_to?) { |symbol| false } + define_method(:respond_to?) { |_symbol| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method) assert_raises(NoMethodError) { mock.invoked_method } end def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder instance = Class.new do - define_method(:respond_to?) { |symbol| false } + define_method(:respond_to?) { |_symbol| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method)