From 0a33bc19dac3c60e3ea1ca3204d73e4e5d026210 Mon Sep 17 00:00:00 2001 From: James Mead Date: Wed, 6 Dec 2023 08:27:46 +0000 Subject: [PATCH] WIP: Acceptance test for stubbing method implemented by method_missing See #634. --- ...thod_implemented_by_method_missing_test.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/acceptance/stubbing_method_implemented_by_method_missing_test.rb diff --git a/test/acceptance/stubbing_method_implemented_by_method_missing_test.rb b/test/acceptance/stubbing_method_implemented_by_method_missing_test.rb new file mode 100644 index 00000000..3b3f4c24 --- /dev/null +++ b/test/acceptance/stubbing_method_implemented_by_method_missing_test.rb @@ -0,0 +1,55 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) + +class StubbingMethodImplementedByMethodMissingTest < Mocha::TestCase + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end + + def test_stubs_method_implemented_using_method_missing + object = Class.new do + def method_missing(symbol, *) + symbol == :foo ? :method_missing_value : super + end + + def respond_to_missing?(symbol, *) + symbol == :foo + end + end.new + test_result = run_as_test do + object.stubs(:foo).returns(:stubbed_value) + assert_equal :stubbed_value, object.foo + end + assert_passed(test_result) + end + + def test_stubs_method_implemented_using_method_missing_when_private_method_with_same_name_exists_on_superclass + superclass = Class.new do + def foo + :superclass_value + end + private :foo + end + object = Class.new(superclass) do + def method_missing(symbol, *) + symbol == :foo ? :method_missing_value : super + end + + def respond_to_missing?(symbol, *) + symbol == :foo + end + end.new + test_result = run_as_test do + object.stubs(:foo).returns(:stubbed_value) + assert_equal :stubbed_value, object.foo + assert_equal :stubbed_value, object.public_send(:foo) + assert_equal :superclass_value, object.send(:foo) + end + assert_passed(test_result) + end +end