From d538675551bbb8b2814286e8bcdba55f53360102 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 18:27:19 +0000 Subject: [PATCH 01/63] Refactor: extract method stub_method_unnecessarily --- .../stubbing_method_unnecessarily_test.rb | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 12ef4fa8f..a1a59a217 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -13,31 +13,19 @@ def teardown end def test_should_allow_stubbing_method_unnecessarily - Mocha.configure { |c| c.stubbing_method_unnecessarily = :allow } - test_result = run_as_test do - mock = mock('mock') - mock.stubs(:public_method) - end + test_result = stub_method_unnecessarily(:allow) assert_passed(test_result) assert !@logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') end def test_should_warn_when_stubbing_method_unnecessarily - Mocha.configure { |c| c.stubbing_method_unnecessarily = :warn } - test_result = run_as_test do - mock = mock('mock') - mock.stubs(:public_method) - end + test_result = stub_method_unnecessarily(:warn) assert_passed(test_result) assert @logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') end def test_should_prevent_stubbing_method_unnecessarily - Mocha.configure { |c| c.stubbing_method_unnecessarily = :prevent } - test_result = run_as_test do - mock = mock('mock') - mock.stubs(:public_method) - end + test_result = stub_method_unnecessarily(:prevent) assert_failed(test_result) assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method unnecessarily: #.public_method(any_parameters)') end @@ -60,4 +48,12 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked end assert_passed(test_result) end + + def stub_method_unnecessarily(treatment) + Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } + run_as_test do + mock = mock('mock') + mock.stubs(:public_method) + end + end end From 356d96782669db5995d62e730f79140ff5f3e1c4 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 18:33:01 +0000 Subject: [PATCH 02/63] Refactor: extract violation message into method --- .../acceptance/stubbing_method_unnecessarily_test.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index a1a59a217..250421cf4 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -15,19 +15,19 @@ def teardown def test_should_allow_stubbing_method_unnecessarily test_result = stub_method_unnecessarily(:allow) assert_passed(test_result) - assert !@logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') + assert !@logger.warnings.include?(violation_message) end def test_should_warn_when_stubbing_method_unnecessarily test_result = stub_method_unnecessarily(:warn) assert_passed(test_result) - assert @logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') + assert @logger.warnings.include?(violation_message) end def test_should_prevent_stubbing_method_unnecessarily test_result = stub_method_unnecessarily(:prevent) assert_failed(test_result) - assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method unnecessarily: #.public_method(any_parameters)') + assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end def test_should_default_to_allow_stubbing_method_unnecessarily @@ -36,7 +36,7 @@ def test_should_default_to_allow_stubbing_method_unnecessarily mock.stubs(:public_method) end assert_passed(test_result) - assert !@logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') + assert !@logger.warnings.include?(violation_message) end def test_should_allow_stubbing_method_when_stubbed_method_is_invoked @@ -56,4 +56,8 @@ def stub_method_unnecessarily(treatment) mock.stubs(:public_method) end end + + def violation_message + 'stubbing method unnecessarily: #.public_method(any_parameters)' + end end From 2b58fe2be01da7fd409c595b73a8bc615c3fa897 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 18:35:09 +0000 Subject: [PATCH 03/63] configure check only if treatment isn't default This allows us to call stub_method_unnecessarily in the default case without a treatment --- test/acceptance/stubbing_method_unnecessarily_test.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 250421cf4..6d4615a99 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -31,10 +31,7 @@ def test_should_prevent_stubbing_method_unnecessarily end def test_should_default_to_allow_stubbing_method_unnecessarily - test_result = run_as_test do - mock = mock('mock') - mock.stubs(:public_method) - end + test_result = stub_method_unnecessarily assert_passed(test_result) assert !@logger.warnings.include?(violation_message) end @@ -49,8 +46,8 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked assert_passed(test_result) end - def stub_method_unnecessarily(treatment) - Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } + def stub_method_unnecessarily(treatment = :default) + Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } unless treatment == :default run_as_test do mock = mock('mock') mock.stubs(:public_method) From 7737927ee4b9c7eb0bc79d8253744e7e2055f92b Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 18:50:01 +0000 Subject: [PATCH 04/63] Refactor: extract method run_test_with_check --- test/acceptance/stubbing_method_unnecessarily_test.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 6d4615a99..1d5233c9f 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -37,8 +37,7 @@ def test_should_default_to_allow_stubbing_method_unnecessarily end def test_should_allow_stubbing_method_when_stubbed_method_is_invoked - Mocha.configure { |c| c.stubbing_method_unnecessarily = :prevent } - test_result = run_as_test do + test_result = run_test_with_check(:prevent) do mock = mock('mock') mock.stubs(:public_method) mock.public_method @@ -47,13 +46,17 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked end def stub_method_unnecessarily(treatment = :default) - Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } unless treatment == :default - run_as_test do + run_test_with_check(treatment) do mock = mock('mock') mock.stubs(:public_method) end end + def run_test_with_check(treatment = :default, &block) + Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } unless treatment == :default + run_as_test(&block) + end + def violation_message 'stubbing method unnecessarily: #.public_method(any_parameters)' end From 68d6abbed240cbfc9754597c54c163216f5c5832 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 18:51:49 +0000 Subject: [PATCH 05/63] Refactor: inline temps --- test/acceptance/stubbing_method_unnecessarily_test.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 1d5233c9f..cffcf90fa 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -13,14 +13,12 @@ def teardown end def test_should_allow_stubbing_method_unnecessarily - test_result = stub_method_unnecessarily(:allow) - assert_passed(test_result) + assert_passed(stub_method_unnecessarily(:allow)) assert !@logger.warnings.include?(violation_message) end def test_should_warn_when_stubbing_method_unnecessarily - test_result = stub_method_unnecessarily(:warn) - assert_passed(test_result) + assert_passed(stub_method_unnecessarily(:warn)) assert @logger.warnings.include?(violation_message) end @@ -31,8 +29,7 @@ def test_should_prevent_stubbing_method_unnecessarily end def test_should_default_to_allow_stubbing_method_unnecessarily - test_result = stub_method_unnecessarily - assert_passed(test_result) + assert_passed(stub_method_unnecessarily) assert !@logger.warnings.include?(violation_message) end From 964262c4b1bfd254e1ae5be8e0bdfe94486b2630 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 19:41:29 +0000 Subject: [PATCH 06/63] define test class rather than body conditionally if RUBY_VERSION < '2.2.0', there's no point defining a test class that does just setup and teardown. Instead, let's not define the test class at all. --- test/acceptance/stubbing_nil_test.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 361083d2e..b70c0f68b 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -1,18 +1,18 @@ require File.expand_path('../acceptance_test_helper', __FILE__) require 'mocha/configuration' -class StubbingNilTest < Mocha::TestCase - include AcceptanceTest +if RUBY_VERSION < '2.2.0' + class StubbingNilTest < Mocha::TestCase + include AcceptanceTest - def setup - setup_acceptance_test - end + def setup + setup_acceptance_test + end - def teardown - teardown_acceptance_test - end + def teardown + teardown_acceptance_test + end - if RUBY_VERSION < '2.2.0' def test_should_allow_stubbing_method_on_nil Mocha.configure { |c| c.stubbing_method_on_nil = :allow } test_result = run_as_test do From d1665a3d4dfeac66019cab44d20f2593f8f3207d Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 19:55:16 +0000 Subject: [PATCH 07/63] extract stub_method_on_nil & violation_message ... following the same structure and logic as in the case of stubbing_method_unnecessarily --- test/acceptance/stubbing_nil_test.rb | 40 +++++++++++++--------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index b70c0f68b..85cd71953 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -14,38 +14,25 @@ def teardown end def test_should_allow_stubbing_method_on_nil - Mocha.configure { |c| c.stubbing_method_on_nil = :allow } - test_result = run_as_test do - nil.stubs(:stubbed_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?('stubbing method on nil: nil.stubbed_method') + assert_passed(stub_method_on_nil(:allow)) + assert !@logger.warnings.include?(violation_message) end def test_should_warn_on_stubbing_method_on_nil - Mocha.configure { |c| c.stubbing_method_on_nil = :warn } - test_result = run_as_test do - nil.stubs(:stubbed_method) - end - assert_passed(test_result) - assert @logger.warnings.include?('stubbing method on nil: nil.stubbed_method') + assert_passed(stub_method_on_nil(:warn)) + assert @logger.warnings.include?(violation_message) end def test_should_prevent_stubbing_method_on_nil - Mocha.configure { |c| c.stubbing_method_on_nil = :prevent } - test_result = run_as_test do - nil.stubs(:stubbed_method) - end + test_result = stub_method_on_nil(:prevent) assert_failed(test_result) - assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method on nil: nil.stubbed_method') + assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end def test_should_default_to_prevent_stubbing_method_on_non_mock_object - test_result = run_as_test do - nil.stubs(:stubbed_method) - end + test_result = stub_method_on_nil assert_failed(test_result) - assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method on nil: nil.stubbed_method') + assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end def test_should_allow_stubbing_method_on_non_nil_object @@ -56,5 +43,16 @@ def test_should_allow_stubbing_method_on_non_nil_object end assert_passed(test_result) end + + def stub_method_on_nil(treatment = :default) + Mocha.configure { |c| c.stubbing_method_on_nil = treatment } unless treatment == :default + run_as_test do + nil.stubs(:stubbed_method) + end + end + + def violation_message + 'stubbing method on nil: nil.stubbed_method' + end end end From 3ec6ac99e057d824b22045f5264d3ce757c662b9 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 9 Jan 2020 19:56:01 +0000 Subject: [PATCH 08/63] Refactor: inline temp --- test/acceptance/stubbing_nil_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 85cd71953..abb6fcc96 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -37,9 +37,8 @@ def test_should_default_to_prevent_stubbing_method_on_non_mock_object def test_should_allow_stubbing_method_on_non_nil_object Mocha.configure { |c| c.stubbing_method_on_nil = :prevent } - object = Object.new test_result = run_as_test do - object.stubs(:stubbed_method) + Object.new.stubs(:stubbed_method) end assert_passed(test_result) end From 73f20ec957187ddc967deffa67f550b38401fd1b Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 06:25:28 +0000 Subject: [PATCH 09/63] Refactor: extract method run_test_with_check --- test/acceptance/stubbing_nil_test.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index abb6fcc96..0b273c041 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -36,20 +36,23 @@ def test_should_default_to_prevent_stubbing_method_on_non_mock_object end def test_should_allow_stubbing_method_on_non_nil_object - Mocha.configure { |c| c.stubbing_method_on_nil = :prevent } - test_result = run_as_test do + test_result = run_test_with_check(:prevent) do Object.new.stubs(:stubbed_method) end assert_passed(test_result) end def stub_method_on_nil(treatment = :default) - Mocha.configure { |c| c.stubbing_method_on_nil = treatment } unless treatment == :default - run_as_test do + run_test_with_check(treatment) do nil.stubs(:stubbed_method) end end + def run_test_with_check(treatment = :default, &block) + Mocha.configure { |c| c.stubbing_method_on_nil = treatment } unless treatment == :default + run_as_test(&block) + end + def violation_message 'stubbing method on nil: nil.stubbed_method' end From 8c40baf65f0c5d47b7e894f2209cc62e834af38a Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 06:40:41 +0000 Subject: [PATCH 10/63] Refactor:rename method_x->with_potential_violation ...to reduce diff between StubbingMethod{Unnecessarily,OnNil}Test to prep to extract common module --- .../stubbing_method_unnecessarily_test.rb | 16 ++++++++-------- test/acceptance/stubbing_nil_test.rb | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index cffcf90fa..1cb8e9b94 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -12,24 +12,24 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_method_unnecessarily - assert_passed(stub_method_unnecessarily(:allow)) + def test_should_allow_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:allow)) assert !@logger.warnings.include?(violation_message) end - def test_should_warn_when_stubbing_method_unnecessarily - assert_passed(stub_method_unnecessarily(:warn)) + def test_should_warn_on_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:warn)) assert @logger.warnings.include?(violation_message) end - def test_should_prevent_stubbing_method_unnecessarily - test_result = stub_method_unnecessarily(:prevent) + def test_should_prevent_stubbing_with_potential_violation + test_result = stub_with_potential_violation(:prevent) assert_failed(test_result) assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end def test_should_default_to_allow_stubbing_method_unnecessarily - assert_passed(stub_method_unnecessarily) + assert_passed(stub_with_potential_violation) assert !@logger.warnings.include?(violation_message) end @@ -42,7 +42,7 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked assert_passed(test_result) end - def stub_method_unnecessarily(treatment = :default) + def stub_with_potential_violation(treatment = :default) run_test_with_check(treatment) do mock = mock('mock') mock.stubs(:public_method) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 0b273c041..2a8356a62 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -13,24 +13,24 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_method_on_nil - assert_passed(stub_method_on_nil(:allow)) + def test_should_allow_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:allow)) assert !@logger.warnings.include?(violation_message) end - def test_should_warn_on_stubbing_method_on_nil - assert_passed(stub_method_on_nil(:warn)) + def test_should_warn_on_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:warn)) assert @logger.warnings.include?(violation_message) end - def test_should_prevent_stubbing_method_on_nil - test_result = stub_method_on_nil(:prevent) + def test_should_prevent_stubbing_with_potential_violation + test_result = stub_with_potential_violation(:prevent) assert_failed(test_result) assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end def test_should_default_to_prevent_stubbing_method_on_non_mock_object - test_result = stub_method_on_nil + test_result = stub_with_potential_violation assert_failed(test_result) assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end @@ -42,7 +42,7 @@ def test_should_allow_stubbing_method_on_non_nil_object assert_passed(test_result) end - def stub_method_on_nil(treatment = :default) + def stub_with_potential_violation(treatment = :default) run_test_with_check(treatment) do nil.stubs(:stubbed_method) end From 7c0c807806f0452f7b71da0710d40824d026fc6d Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 06:47:24 +0000 Subject: [PATCH 11/63] Refactor: extract block to potential_violation ...to furter reduce diff betwee StubbingMethod{Unnecessarily,OnNil}Test to prep to extract common module --- test/acceptance/stubbing_method_unnecessarily_test.rb | 10 ++++++---- test/acceptance/stubbing_nil_test.rb | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 1cb8e9b94..62552e780 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -43,10 +43,7 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked end def stub_with_potential_violation(treatment = :default) - run_test_with_check(treatment) do - mock = mock('mock') - mock.stubs(:public_method) - end + run_test_with_check(treatment, &method(:potential_violation)) end def run_test_with_check(treatment = :default, &block) @@ -57,4 +54,9 @@ def run_test_with_check(treatment = :default, &block) def violation_message 'stubbing method unnecessarily: #.public_method(any_parameters)' end + + def potential_violation + mock = mock('mock') + mock.stubs(:public_method) + end end diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 2a8356a62..736cf8f04 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -43,9 +43,7 @@ def test_should_allow_stubbing_method_on_non_nil_object end def stub_with_potential_violation(treatment = :default) - run_test_with_check(treatment) do - nil.stubs(:stubbed_method) - end + run_test_with_check(treatment, &method(:potential_violation)) end def run_test_with_check(treatment = :default, &block) @@ -56,5 +54,9 @@ def run_test_with_check(treatment = :default, &block) def violation_message 'stubbing method on nil: nil.stubbed_method' end + + def potential_violation + nil.stubs(:stubbed_method) + end end end From 34f90ae1fa4b21c046d3101149b2463e200658d9 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 06:51:29 +0000 Subject: [PATCH 12/63] Refactor: extract block to configure_violation ...to furter reduce diff betwee StubbingMethod{Unnecessarily,OnNil}Test to prep to extract common module --- test/acceptance/stubbing_method_unnecessarily_test.rb | 6 +++++- test/acceptance/stubbing_nil_test.rb | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 62552e780..ad3c823a1 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -47,7 +47,7 @@ def stub_with_potential_violation(treatment = :default) end def run_test_with_check(treatment = :default, &block) - Mocha.configure { |c| c.stubbing_method_unnecessarily = treatment } unless treatment == :default + Mocha.configure { |c| configure_violation(c, treatment) } unless treatment == :default run_as_test(&block) end @@ -59,4 +59,8 @@ def potential_violation mock = mock('mock') mock.stubs(:public_method) end + + def configure_violation(config, treatment) + config.stubbing_method_unnecessarily = treatment + end end diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 736cf8f04..a1c27c05a 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -47,7 +47,7 @@ def stub_with_potential_violation(treatment = :default) end def run_test_with_check(treatment = :default, &block) - Mocha.configure { |c| c.stubbing_method_on_nil = treatment } unless treatment == :default + Mocha.configure { |c| configure_violation(c, treatment) } unless treatment == :default run_as_test(&block) end @@ -58,5 +58,9 @@ def violation_message def potential_violation nil.stubs(:stubbed_method) end + + def configure_violation(config, treatment) + config.stubbing_method_on_nil = treatment + end end end From c744731872ba1358fa9d7bb41f88081f55c1a261 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:04:26 +0000 Subject: [PATCH 13/63] Refactor: make test method name truthful This seems to have been an artifact of copy-paste --- test/acceptance/stubbing_nil_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index a1c27c05a..c21711e4e 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -29,7 +29,7 @@ def test_should_prevent_stubbing_with_potential_violation assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") end - def test_should_default_to_prevent_stubbing_method_on_non_mock_object + def test_should_default_to_prevent_stubbing_method_on_nil test_result = stub_with_potential_violation assert_failed(test_result) assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") From be0831a02126e4a8100eed909b7aba78cb95acb6 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:10:52 +0000 Subject: [PATCH 14/63] extract StubbingWithPotentialViolationSharedTests ... pulling up all the identical methods between Stubbing{MethodUnnecessarily,Nil}Test --- .../stubbing_method_unnecessarily_test.rb | 38 +----------------- test/acceptance/stubbing_nil_test.rb | 38 +----------------- ...g_with_potential_violation_shared_tests.rb | 39 +++++++++++++++++++ 3 files changed, 43 insertions(+), 72 deletions(-) create mode 100644 test/acceptance/stubbing_with_potential_violation_shared_tests.rb diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index ad3c823a1..4d5401b5a 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -1,32 +1,7 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingMethodUnnecessarilyTest < Mocha::TestCase - include AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_with_potential_violation - assert_passed(stub_with_potential_violation(:allow)) - assert !@logger.warnings.include?(violation_message) - end - - def test_should_warn_on_stubbing_with_potential_violation - assert_passed(stub_with_potential_violation(:warn)) - assert @logger.warnings.include?(violation_message) - end - - def test_should_prevent_stubbing_with_potential_violation - test_result = stub_with_potential_violation(:prevent) - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") - end + include StubbingWithPotentialViolationSharedTests def test_should_default_to_allow_stubbing_method_unnecessarily assert_passed(stub_with_potential_violation) @@ -42,15 +17,6 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked assert_passed(test_result) end - def stub_with_potential_violation(treatment = :default) - run_test_with_check(treatment, &method(:potential_violation)) - end - - def run_test_with_check(treatment = :default, &block) - Mocha.configure { |c| configure_violation(c, treatment) } unless treatment == :default - run_as_test(&block) - end - def violation_message 'stubbing method unnecessarily: #.public_method(any_parameters)' end diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index c21711e4e..6168bff0e 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -1,33 +1,8 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) if RUBY_VERSION < '2.2.0' class StubbingNilTest < Mocha::TestCase - include AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_with_potential_violation - assert_passed(stub_with_potential_violation(:allow)) - assert !@logger.warnings.include?(violation_message) - end - - def test_should_warn_on_stubbing_with_potential_violation - assert_passed(stub_with_potential_violation(:warn)) - assert @logger.warnings.include?(violation_message) - end - - def test_should_prevent_stubbing_with_potential_violation - test_result = stub_with_potential_violation(:prevent) - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") - end + include StubbingWithPotentialViolationSharedTests def test_should_default_to_prevent_stubbing_method_on_nil test_result = stub_with_potential_violation @@ -42,15 +17,6 @@ def test_should_allow_stubbing_method_on_non_nil_object assert_passed(test_result) end - def stub_with_potential_violation(treatment = :default) - run_test_with_check(treatment, &method(:potential_violation)) - end - - def run_test_with_check(treatment = :default, &block) - Mocha.configure { |c| configure_violation(c, treatment) } unless treatment == :default - run_as_test(&block) - end - def violation_message 'stubbing method on nil: nil.stubbed_method' end diff --git a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb new file mode 100644 index 000000000..d9bf3b959 --- /dev/null +++ b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb @@ -0,0 +1,39 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) +require 'mocha/configuration' + +module StubbingWithPotentialViolationSharedTests + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end + + def test_should_allow_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:allow)) + assert !@logger.warnings.include?(violation_message) + end + + def test_should_warn_on_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:warn)) + assert @logger.warnings.include?(violation_message) + end + + def test_should_prevent_stubbing_with_potential_violation + test_result = stub_with_potential_violation(:prevent) + assert_failed(test_result) + assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") + end + + def stub_with_potential_violation(treatment = :default) + run_test_with_check(treatment, &method(:potential_violation)) + end + + def run_test_with_check(treatment = :default, &block) + Mocha.configure { |c| configure_violation(c, treatment) } unless treatment == :default + run_as_test(&block) + end +end From d1360006fcd51c93a93e72eeb29988ab0298ed88 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:15:08 +0000 Subject: [PATCH 15/63] Refactor: reorder methods for expressiveness --- .../stubbing_method_unnecessarily_test.rb | 26 +++++++++---------- test/acceptance/stubbing_nil_test.rb | 24 ++++++++--------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 4d5401b5a..8cadc547d 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -3,6 +3,19 @@ class StubbingMethodUnnecessarilyTest < Mocha::TestCase include StubbingWithPotentialViolationSharedTests + def configure_violation(config, treatment) + config.stubbing_method_unnecessarily = treatment + end + + def potential_violation + mock = mock('mock') + mock.stubs(:public_method) + end + + def violation_message + 'stubbing method unnecessarily: #.public_method(any_parameters)' + end + def test_should_default_to_allow_stubbing_method_unnecessarily assert_passed(stub_with_potential_violation) assert !@logger.warnings.include?(violation_message) @@ -16,17 +29,4 @@ def test_should_allow_stubbing_method_when_stubbed_method_is_invoked end assert_passed(test_result) end - - def violation_message - 'stubbing method unnecessarily: #.public_method(any_parameters)' - end - - def potential_violation - mock = mock('mock') - mock.stubs(:public_method) - end - - def configure_violation(config, treatment) - config.stubbing_method_unnecessarily = treatment - end end diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 6168bff0e..6a112a774 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -4,6 +4,18 @@ class StubbingNilTest < Mocha::TestCase include StubbingWithPotentialViolationSharedTests + def configure_violation(config, treatment) + config.stubbing_method_on_nil = treatment + end + + def potential_violation + nil.stubs(:stubbed_method) + end + + def violation_message + 'stubbing method on nil: nil.stubbed_method' + end + def test_should_default_to_prevent_stubbing_method_on_nil test_result = stub_with_potential_violation assert_failed(test_result) @@ -16,17 +28,5 @@ def test_should_allow_stubbing_method_on_non_nil_object end assert_passed(test_result) end - - def violation_message - 'stubbing method on nil: nil.stubbed_method' - end - - def potential_violation - nil.stubs(:stubbed_method) - end - - def configure_violation(config, treatment) - config.stubbing_method_on_nil = treatment - end end end From cfb2d60fa19a59985956eb9a39449e8039ace8a0 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:16:46 +0000 Subject: [PATCH 16/63] Refactor: rename for expressiveness & consistency --- test/acceptance/stubbing_method_unnecessarily_test.rb | 4 ++-- test/acceptance/stubbing_nil_test.rb | 4 ++-- .../stubbing_with_potential_violation_shared_tests.rb | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 8cadc547d..964b16ce4 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -12,13 +12,13 @@ def potential_violation mock.stubs(:public_method) end - def violation_message + def message_on_violation 'stubbing method unnecessarily: #.public_method(any_parameters)' end def test_should_default_to_allow_stubbing_method_unnecessarily assert_passed(stub_with_potential_violation) - assert !@logger.warnings.include?(violation_message) + assert !@logger.warnings.include?(message_on_violation) end def test_should_allow_stubbing_method_when_stubbed_method_is_invoked diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 6a112a774..cd09dc420 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -12,14 +12,14 @@ def potential_violation nil.stubs(:stubbed_method) end - def violation_message + def message_on_violation 'stubbing method on nil: nil.stubbed_method' end def test_should_default_to_prevent_stubbing_method_on_nil test_result = stub_with_potential_violation assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") + assert test_result.error_messages.include?("Mocha::StubbingError: #{message_on_violation}") end def test_should_allow_stubbing_method_on_non_nil_object diff --git a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb index d9bf3b959..cfe93eaf1 100644 --- a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb +++ b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb @@ -14,18 +14,18 @@ def teardown def test_should_allow_stubbing_with_potential_violation assert_passed(stub_with_potential_violation(:allow)) - assert !@logger.warnings.include?(violation_message) + assert !@logger.warnings.include?(message_on_violation) end def test_should_warn_on_stubbing_with_potential_violation assert_passed(stub_with_potential_violation(:warn)) - assert @logger.warnings.include?(violation_message) + assert @logger.warnings.include?(message_on_violation) end def test_should_prevent_stubbing_with_potential_violation test_result = stub_with_potential_violation(:prevent) assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: #{violation_message}") + assert test_result.error_messages.include?("Mocha::StubbingError: #{message_on_violation}") end def stub_with_potential_violation(treatment = :default) From 9071d2ad3938c36404ed2930f5e0b1415942b970 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:34:15 +0000 Subject: [PATCH 17/63] use shared tests in StubbingNonExistentAnyInstanceMethodTest We can now start using the 'framework' we've extracted in the form of a shared test module customizable via configure_violation, potenial_violation and message_on_violation. StubbingNonExistentAnyInstanceMethodTest varies only in those 3 things. It checks the same things as in the shared module (viz. allow, warn, prevent and default). --- ...g_non_existent_any_instance_method_test.rb | 48 +++++-------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index fe518cc37..1c4029a2f 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,54 +1,28 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationSharedTests def setup - setup_acceptance_test + super + @klass = Class.new end - def teardown - teardown_acceptance_test + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment end - def test_should_allow_stubbing_non_existent_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :allow } - klass = Class.new - test_result = run_as_test do - klass.any_instance.stubs(:non_existent_method) - end - assert !@logger.warnings.include?("stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") - assert_passed(test_result) + def potential_violation + @klass.any_instance.stubs(:non_existent_method) end - def test_should_warn_when_stubbing_non_existent_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :warn } - klass = Class.new - test_result = run_as_test do - klass.any_instance.stubs(:non_existent_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") - end - - def test_should_prevent_stubbing_non_existent_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new - test_result = run_as_test do - klass.any_instance.stubs(:non_existent_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") + def message_on_violation + "stubbing non-existent method: #{@klass.any_instance.mocha_inspect}.non_existent_method" end def test_should_default_to_allow_stubbing_non_existent_any_instance_method - klass = Class.new - test_result = run_as_test do - klass.any_instance.stubs(:non_existent_method) - end + assert_passed(stub_with_potential_violation) assert !@logger.warnings.include?("stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") - assert_passed(test_result) end def test_should_allow_stubbing_existing_public_any_instance_method From 88e50e426baf977d6689c2a629448a374802746a Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 07:44:02 +0000 Subject: [PATCH 18/63] Refactor: extract common assertion into shared mod --- test/acceptance/stubbing_method_unnecessarily_test.rb | 3 +-- .../stubbing_non_existent_any_instance_method_test.rb | 3 +-- .../stubbing_with_potential_violation_shared_tests.rb | 5 +++++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 964b16ce4..72cea55c1 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -17,8 +17,7 @@ def message_on_violation end def test_should_default_to_allow_stubbing_method_unnecessarily - assert_passed(stub_with_potential_violation) - assert !@logger.warnings.include?(message_on_violation) + assert_defaults_to_allow_stubbing_with_potential_violation end def test_should_allow_stubbing_method_when_stubbed_method_is_invoked diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 1c4029a2f..5d256b24e 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,8 +21,7 @@ def message_on_violation end def test_should_default_to_allow_stubbing_non_existent_any_instance_method - assert_passed(stub_with_potential_violation) - assert !@logger.warnings.include?("stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") + assert_defaults_to_allow_stubbing_with_potential_violation end def test_should_allow_stubbing_existing_public_any_instance_method diff --git a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb index cfe93eaf1..cd386141f 100644 --- a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb +++ b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb @@ -28,6 +28,11 @@ def test_should_prevent_stubbing_with_potential_violation assert test_result.error_messages.include?("Mocha::StubbingError: #{message_on_violation}") end + def assert_defaults_to_allow_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation) + assert !@logger.warnings.include?(message_on_violation) + end + def stub_with_potential_violation(treatment = :default) run_test_with_check(treatment, &method(:potential_violation)) end From 451ec0b9d4cd0b5b309df3d8c675b9496ff1a779 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 08:28:54 +0000 Subject: [PATCH 19/63] use shared tests in StubbingNonExistentClassMethodTest ... just as in the case of StubbingNonExistentAnyInstanceMethodTest --- ...stubbing_non_existent_class_method_test.rb | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index 4c9ad538e..3a5ca33ce 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,54 +1,27 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationSharedTests def setup - setup_acceptance_test + super + @klass = Class.new end - def teardown - teardown_acceptance_test + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment end - def test_should_allow_stubbing_non_existent_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :allow } - klass = Class.new - test_result = run_as_test do - klass.stubs(:non_existent_method) - end - assert !@logger.warnings.include?("stubbing non-existent method: #{klass.mocha_inspect}.non_existent_method") - assert_passed(test_result) - end - - def test_should_warn_when_stubbing_non_existent_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :warn } - klass = Class.new - test_result = run_as_test do - klass.stubs(:non_existent_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-existent method: #{klass.mocha_inspect}.non_existent_method") + def potential_violation + @klass.stubs(:non_existent_method) end - def test_should_prevent_stubbing_non_existent_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new - test_result = run_as_test do - klass.stubs(:non_existent_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{klass.mocha_inspect}.non_existent_method") + def message_on_violation + "stubbing non-existent method: #{@klass.mocha_inspect}.non_existent_method" end def test_should_default_to_allow_stubbing_non_existent_class_method - klass = Class.new - test_result = run_as_test do - klass.stubs(:non_existent_method) - end - assert !@logger.warnings.include?("stubbing non-existent method: #{klass.mocha_inspect}.non_existent_method") - assert_passed(test_result) + assert_defaults_to_allow_stubbing_with_potential_violation end # rubocop:disable Lint/DuplicateMethods From f16f1b3a597ba82c73bd1925cfa21d3d7ab5e10a Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 09:08:02 +0000 Subject: [PATCH 20/63] use shared tests in StubbingNonExistentInstanceMethodTest ... just as in case of StubbingNonExistent{AnyInstance,Class}MethodTest --- ...bbing_non_existent_instance_method_test.rb | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 8ce7841ed..ada8b61bf 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,54 +1,27 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationSharedTests def setup - setup_acceptance_test + super + @instance = Class.new.new end - def teardown - teardown_acceptance_test + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment end - def test_should_allow_stubbing_non_existent_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :allow } - instance = Class.new.new - test_result = run_as_test do - instance.stubs(:non_existent_method) - end - assert !@logger.warnings.include?("stubbing non-existent method: #{instance.mocha_inspect}.non_existent_method") - assert_passed(test_result) - end - - def test_should_warn_when_stubbing_non_existent_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :warn } - instance = Class.new.new - test_result = run_as_test do - instance.stubs(:non_existent_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-existent method: #{instance.mocha_inspect}.non_existent_method") + def potential_violation + @instance.stubs(:non_existent_method) end - def test_should_prevent_stubbing_non_existent_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - instance = Class.new.new - test_result = run_as_test do - instance.stubs(:non_existent_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{instance.mocha_inspect}.non_existent_method") + def message_on_violation + "stubbing non-existent method: #{@instance.mocha_inspect}.non_existent_method" end def test_should_default_to_allow_stubbing_non_existent_instance_method - instance = Class.new.new - test_result = run_as_test do - instance.stubs(:non_existent_method) - end - assert !@logger.warnings.include?("stubbing non-existent method: #{instance.mocha_inspect}.non_existent_method") - assert_passed(test_result) + assert_defaults_to_allow_stubbing_with_potential_violation end def test_should_allow_stubbing_existing_public_instance_method From ad2cd32fdd9157865b1237147a7f02b1ac8960bb Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 11:02:03 +0000 Subject: [PATCH 21/63] use shared tests in StubbingOnNonMockObjectTest ... just as in all the StubbingNonExistent*MethodTest --- .../stubbing_on_non_mock_object_test.rb | 59 ++++--------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/test/acceptance/stubbing_on_non_mock_object_test.rb b/test/acceptance/stubbing_on_non_mock_object_test.rb index d1a08d2a7..e7965c6fc 100644 --- a/test/acceptance/stubbing_on_non_mock_object_test.rb +++ b/test/acceptance/stubbing_on_non_mock_object_test.rb @@ -1,62 +1,29 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingOnNonMockObjectTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationSharedTests def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_method_on_non_mock_object - Mocha.configure { |c| c.stubbing_method_on_non_mock_object = :allow } - non_mock_object = Class.new do + super + @non_mock_object = Class.new do def existing_method; end end - test_result = run_as_test do - non_mock_object.stubs(:existing_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing method on non-mock object: #{non_mock_object.mocha_inspect}.existing_method") end - def test_should_warn_on_stubbing_method_on_non_mock_object - Mocha.configure { |c| c.stubbing_method_on_non_mock_object = :warn } - non_mock_object = Class.new do - def existing_method; end - end - test_result = run_as_test do - non_mock_object.stubs(:existing_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing method on non-mock object: #{non_mock_object.mocha_inspect}.existing_method") + def configure_violation(config, treatment) + config.stubbing_method_on_non_mock_object = treatment end - def test_should_prevent_stubbing_method_on_non_mock_object - Mocha.configure { |c| c.stubbing_method_on_non_mock_object = :prevent } - non_mock_object = Class.new do - def existing_method; end - end - test_result = run_as_test do - non_mock_object.stubs(:existing_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on non-mock object: #{non_mock_object.mocha_inspect}.existing_method") + def potential_violation + @non_mock_object.stubs(:existing_method) + end + + def message_on_violation + "stubbing method on non-mock object: #{@non_mock_object.mocha_inspect}.existing_method" end def test_should_default_to_allow_stubbing_method_on_non_mock_object - non_mock_object = Class.new do - def existing_method; end - end - test_result = run_as_test do - non_mock_object.stubs(:existing_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing method on non-mock object: #{non_mock_object.mocha_inspect}.existing_method") + assert_defaults_to_allow_stubbing_with_potential_violation end def test_should_allow_stubbing_method_on_mock_object From d67f0b5b0c58da948b9dd6d809ea72bfc29e1fea Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 11:53:01 +0000 Subject: [PATCH 22/63] extract default to allow test to a module so we can include the module rather than a test calling the same assertion in multiple test classes --- .../stubbing_method_unnecessarily_test.rb | 6 +----- ...ubbing_non_existent_any_instance_method_test.rb | 6 +----- .../stubbing_non_existent_class_method_test.rb | 6 +----- .../stubbing_non_existent_instance_method_test.rb | 6 +----- .../acceptance/stubbing_on_non_mock_object_test.rb | 6 +----- ...ubbing_with_potential_violation_shared_tests.rb | 14 +++++++++----- 6 files changed, 14 insertions(+), 30 deletions(-) diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 72cea55c1..9cdab2a1c 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -1,7 +1,7 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingMethodUnnecessarilyTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def configure_violation(config, treatment) config.stubbing_method_unnecessarily = treatment @@ -16,10 +16,6 @@ def message_on_violation 'stubbing method unnecessarily: #.public_method(any_parameters)' end - def test_should_default_to_allow_stubbing_method_unnecessarily - assert_defaults_to_allow_stubbing_with_potential_violation - end - def test_should_allow_stubbing_method_when_stubbed_method_is_invoked test_result = run_test_with_check(:prevent) do mock = mock('mock') diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 5d256b24e..6b3827c6e 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,7 +1,7 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def setup super @@ -20,10 +20,6 @@ def message_on_violation "stubbing non-existent method: #{@klass.any_instance.mocha_inspect}.non_existent_method" end - def test_should_default_to_allow_stubbing_non_existent_any_instance_method - assert_defaults_to_allow_stubbing_with_potential_violation - end - def test_should_allow_stubbing_existing_public_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index 3a5ca33ce..341692fcd 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,7 +1,7 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def setup super @@ -20,10 +20,6 @@ def message_on_violation "stubbing non-existent method: #{@klass.mocha_inspect}.non_existent_method" end - def test_should_default_to_allow_stubbing_non_existent_class_method - assert_defaults_to_allow_stubbing_with_potential_violation - end - # rubocop:disable Lint/DuplicateMethods def test_should_allow_stubbing_existing_public_class_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index ada8b61bf..abf7eb8a4 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,7 +1,7 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def setup super @@ -20,10 +20,6 @@ def message_on_violation "stubbing non-existent method: #{@instance.mocha_inspect}.non_existent_method" end - def test_should_default_to_allow_stubbing_non_existent_instance_method - assert_defaults_to_allow_stubbing_with_potential_violation - end - def test_should_allow_stubbing_existing_public_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do diff --git a/test/acceptance/stubbing_on_non_mock_object_test.rb b/test/acceptance/stubbing_on_non_mock_object_test.rb index e7965c6fc..2c6909c04 100644 --- a/test/acceptance/stubbing_on_non_mock_object_test.rb +++ b/test/acceptance/stubbing_on_non_mock_object_test.rb @@ -1,7 +1,7 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingOnNonMockObjectTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def setup super @@ -22,10 +22,6 @@ def message_on_violation "stubbing method on non-mock object: #{@non_mock_object.mocha_inspect}.existing_method" end - def test_should_default_to_allow_stubbing_method_on_non_mock_object - assert_defaults_to_allow_stubbing_with_potential_violation - end - def test_should_allow_stubbing_method_on_mock_object Mocha.configure { |c| c.stubbing_method_on_non_mock_object = :prevent } test_result = run_as_test do diff --git a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb index cd386141f..1c19c8d08 100644 --- a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb +++ b/test/acceptance/stubbing_with_potential_violation_shared_tests.rb @@ -28,11 +28,6 @@ def test_should_prevent_stubbing_with_potential_violation assert test_result.error_messages.include?("Mocha::StubbingError: #{message_on_violation}") end - def assert_defaults_to_allow_stubbing_with_potential_violation - assert_passed(stub_with_potential_violation) - assert !@logger.warnings.include?(message_on_violation) - end - def stub_with_potential_violation(treatment = :default) run_test_with_check(treatment, &method(:potential_violation)) end @@ -42,3 +37,12 @@ def run_test_with_check(treatment = :default, &block) run_as_test(&block) end end + +module StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingWithPotentialViolationSharedTests + + def test_should_default_to_allow_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation) + assert !@logger.warnings.include?(message_on_violation) + end +end From 420aec39ca8b22db967e4e57f4564489490a4e4e Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 11:59:59 +0000 Subject: [PATCH 23/63] Refactor: rename existing*method->existing_method ... to reduce diff between and prep to DRY up test_should_allow_stubbing_existing_*_any_instance_method --- ...g_non_existent_any_instance_method_test.rb | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 6b3827c6e..10e71c23d 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -23,11 +23,11 @@ def message_on_violation def test_should_allow_stubbing_existing_public_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do - def existing_public_method; end - public :existing_public_method + def existing_method; end + public :existing_method end test_result = run_as_test do - klass.any_instance.stubs(:existing_public_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end @@ -64,11 +64,11 @@ def respond_to?(method, _include_private = false) def test_should_allow_stubbing_existing_protected_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do - def existing_protected_method; end - protected :existing_protected_method + def existing_method; end + protected :existing_method end test_result = run_as_test do - klass.any_instance.stubs(:existing_protected_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end @@ -76,11 +76,11 @@ def existing_protected_method; end def test_should_allow_stubbing_existing_private_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do - def existing_private_method; end - private :existing_private_method + def existing_method; end + private :existing_method end test_result = run_as_test do - klass.any_instance.stubs(:existing_private_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end @@ -88,12 +88,12 @@ def existing_private_method; end def test_should_allow_stubbing_existing_public_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do - def existing_public_method; end - public :existing_public_method + def existing_method; end + public :existing_method end klass = Class.new(superklass) test_result = run_as_test do - klass.any_instance.stubs(:existing_public_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end @@ -101,12 +101,12 @@ def existing_public_method; end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do - def existing_protected_method; end - protected :existing_protected_method + def existing_method; end + protected :existing_method end klass = Class.new(superklass) test_result = run_as_test do - klass.any_instance.stubs(:existing_protected_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end @@ -114,12 +114,12 @@ def existing_protected_method; end def test_should_allow_stubbing_existing_private_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do - def existing_private_method; end - private :existing_private_method + def existing_method; end + private :existing_method end klass = Class.new(superklass) test_result = run_as_test do - klass.any_instance.stubs(:existing_private_method) + klass.any_instance.stubs(:existing_method) end assert_passed(test_result) end From e53c6a84e58d43ae85b4ca714c750a97bdff50dd Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 12:05:24 +0000 Subject: [PATCH 24/63] set visibility programmatically than declaratively ... this will allow us to extract a parameterizable method to DRY up --- ...stubbing_non_existent_any_instance_method_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 10e71c23d..5828a3ebd 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -24,8 +24,8 @@ def test_should_allow_stubbing_existing_public_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end - public :existing_method end + klass.send(:public, :existing_method) test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -65,8 +65,8 @@ def test_should_allow_stubbing_existing_protected_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end - protected :existing_method end + klass.send(:protected, :existing_method) test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -77,8 +77,8 @@ def test_should_allow_stubbing_existing_private_any_instance_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end - private :existing_method end + klass.send(:private, :existing_method) test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -89,8 +89,8 @@ def test_should_allow_stubbing_existing_public_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end - public :existing_method end + superklass.send(:public, :existing_method) klass = Class.new(superklass) test_result = run_as_test do klass.any_instance.stubs(:existing_method) @@ -102,8 +102,8 @@ def test_should_allow_stubbing_existing_protected_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end - protected :existing_method end + superklass.send(:protected, :existing_method) klass = Class.new(superklass) test_result = run_as_test do klass.any_instance.stubs(:existing_method) @@ -115,8 +115,8 @@ def test_should_allow_stubbing_existing_private_any_instance_superclass_method Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end - private :existing_method end + superklass.send(:private, :existing_method) klass = Class.new(superklass) test_result = run_as_test do klass.any_instance.stubs(:existing_method) From 8f403ec26183eaa9c3f07f6a5643b82b7ab5bec2 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 16:06:45 +0000 Subject: [PATCH 25/63] put mocha configure and run_as_test together ... to prep for extracting both into a single method --- ...ubbing_non_existent_any_instance_method_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 5828a3ebd..38f8724d4 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,11 +21,11 @@ def message_on_violation end def test_should_allow_stubbing_existing_public_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end end klass.send(:public, :existing_method) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -33,12 +33,12 @@ def existing_method; end end def test_should_allow_stubbing_method_to_which_any_instance_responds - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def respond_to?(method, _include_private = false) (method == :method_to_which_instance_responds) end end + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:method_to_which_instance_responds) end @@ -62,11 +62,11 @@ def respond_to?(method, _include_private = false) end def test_should_allow_stubbing_existing_protected_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end end klass.send(:protected, :existing_method) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -74,11 +74,11 @@ def existing_method; end end def test_should_allow_stubbing_existing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } klass = Class.new do def existing_method; end end klass.send(:private, :existing_method) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -86,12 +86,12 @@ def existing_method; end end def test_should_allow_stubbing_existing_public_any_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end end superklass.send(:public, :existing_method) klass = Class.new(superklass) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -99,12 +99,12 @@ def existing_method; end end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end end superklass.send(:protected, :existing_method) klass = Class.new(superklass) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end @@ -112,12 +112,12 @@ def existing_method; end end def test_should_allow_stubbing_existing_private_any_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } superklass = Class.new do def existing_method; end end superklass.send(:private, :existing_method) klass = Class.new(superklass) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) end From 23f11a5ce3dd4e29f490fae68404edd80d7e47c0 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 16:09:47 +0000 Subject: [PATCH 26/63] extract assert_allows_stubbing_existing_any_instance_method --- ...g_non_existent_any_instance_method_test.rb | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 38f8724d4..d551e0b42 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -25,11 +25,7 @@ def test_should_allow_stubbing_existing_public_any_instance_method def existing_method; end end klass.send(:public, :existing_method) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) + assert_allows_stubbing_existing_any_instance_method(klass) end def test_should_allow_stubbing_method_to_which_any_instance_responds @@ -66,11 +62,7 @@ def test_should_allow_stubbing_existing_protected_any_instance_method def existing_method; end end klass.send(:protected, :existing_method) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) + assert_allows_stubbing_existing_any_instance_method(klass) end def test_should_allow_stubbing_existing_private_any_instance_method @@ -78,11 +70,7 @@ def test_should_allow_stubbing_existing_private_any_instance_method def existing_method; end end klass.send(:private, :existing_method) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) + assert_allows_stubbing_existing_any_instance_method(klass) end def test_should_allow_stubbing_existing_public_any_instance_superclass_method @@ -90,12 +78,7 @@ def test_should_allow_stubbing_existing_public_any_instance_superclass_method def existing_method; end end superklass.send(:public, :existing_method) - klass = Class.new(superklass) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) + assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method @@ -103,12 +86,7 @@ def test_should_allow_stubbing_existing_protected_any_instance_superclass_method def existing_method; end end superklass.send(:protected, :existing_method) - klass = Class.new(superklass) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) + assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) end def test_should_allow_stubbing_existing_private_any_instance_superclass_method @@ -116,7 +94,10 @@ def test_should_allow_stubbing_existing_private_any_instance_superclass_method def existing_method; end end superklass.send(:private, :existing_method) - klass = Class.new(superklass) + assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) + end + + def assert_allows_stubbing_existing_any_instance_method(klass) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } test_result = run_as_test do klass.any_instance.stubs(:existing_method) From 9f6164e7c0512e3500eb911b373601bba39d1665 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 16:16:40 +0000 Subject: [PATCH 27/63] Refactor: extract method class_with_method --- ...g_non_existent_any_instance_method_test.rb | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index d551e0b42..af7182180 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,10 +21,7 @@ def message_on_violation end def test_should_allow_stubbing_existing_public_any_instance_method - klass = Class.new do - def existing_method; end - end - klass.send(:public, :existing_method) + klass = class_with_method(:public) assert_allows_stubbing_existing_any_instance_method(klass) end @@ -58,42 +55,27 @@ def respond_to?(method, _include_private = false) end def test_should_allow_stubbing_existing_protected_any_instance_method - klass = Class.new do - def existing_method; end - end - klass.send(:protected, :existing_method) + klass = class_with_method(:protected) assert_allows_stubbing_existing_any_instance_method(klass) end def test_should_allow_stubbing_existing_private_any_instance_method - klass = Class.new do - def existing_method; end - end - klass.send(:private, :existing_method) + klass = class_with_method(:private) assert_allows_stubbing_existing_any_instance_method(klass) end def test_should_allow_stubbing_existing_public_any_instance_superclass_method - superklass = Class.new do - def existing_method; end - end - superklass.send(:public, :existing_method) + superklass = class_with_method(:public) assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method - superklass = Class.new do - def existing_method; end - end - superklass.send(:protected, :existing_method) + superklass = class_with_method(:protected) assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) end def test_should_allow_stubbing_existing_private_any_instance_superclass_method - superklass = Class.new do - def existing_method; end - end - superklass.send(:private, :existing_method) + superklass = class_with_method(:private) assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) end @@ -104,4 +86,12 @@ def assert_allows_stubbing_existing_any_instance_method(klass) end assert_passed(test_result) end + + def class_with_method(visibility) + klass = Class.new do + def existing_method; end + end + klass.send(visibility, :existing_method) + klass + end end From 307aa5aabd67dcd52ac1a05e92a24ec258cc5f45 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 16:18:23 +0000 Subject: [PATCH 28/63] Refactor: inline temps --- ...ng_non_existent_any_instance_method_test.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index af7182180..8ddf0a075 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,8 +21,7 @@ def message_on_violation end def test_should_allow_stubbing_existing_public_any_instance_method - klass = class_with_method(:public) - assert_allows_stubbing_existing_any_instance_method(klass) + assert_allows_stubbing_existing_any_instance_method(class_with_method(:public)) end def test_should_allow_stubbing_method_to_which_any_instance_responds @@ -55,28 +54,23 @@ def respond_to?(method, _include_private = false) end def test_should_allow_stubbing_existing_protected_any_instance_method - klass = class_with_method(:protected) - assert_allows_stubbing_existing_any_instance_method(klass) + assert_allows_stubbing_existing_any_instance_method(class_with_method(:protected)) end def test_should_allow_stubbing_existing_private_any_instance_method - klass = class_with_method(:private) - assert_allows_stubbing_existing_any_instance_method(klass) + assert_allows_stubbing_existing_any_instance_method(class_with_method(:private)) end def test_should_allow_stubbing_existing_public_any_instance_superclass_method - superklass = class_with_method(:public) - assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) + assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:public))) end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method - superklass = class_with_method(:protected) - assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) + assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:protected))) end def test_should_allow_stubbing_existing_private_any_instance_superclass_method - superklass = class_with_method(:private) - assert_allows_stubbing_existing_any_instance_method(Class.new(superklass)) + assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:private))) end def assert_allows_stubbing_existing_any_instance_method(klass) From bbcdb078637ea9e3ebceaac2848aa546195e3b00 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 19:28:48 +0000 Subject: [PATCH 29/63] separate tests for allow stubbing existing methods ... since those tests don't use the same fixture and 'framework' as the tests for checking stubbing non-existent methods, and the class and superclass tests follow the same structure, so separating them highlights the similarity and affords an opportunity for abstraction. --- ...g_non_existent_any_instance_method_test.rb | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 8ddf0a075..03c4e5221 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -19,6 +19,18 @@ def potential_violation def message_on_violation "stubbing non-existent method: #{@klass.any_instance.mocha_inspect}.non_existent_method" end +end + +class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end def test_should_allow_stubbing_existing_public_any_instance_method assert_allows_stubbing_existing_any_instance_method(class_with_method(:public)) @@ -61,6 +73,34 @@ def test_should_allow_stubbing_existing_private_any_instance_method assert_allows_stubbing_existing_any_instance_method(class_with_method(:private)) end + def assert_allows_stubbing_existing_any_instance_method(klass) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + test_result = run_as_test do + klass.any_instance.stubs(:existing_method) + end + assert_passed(test_result) + end + + def class_with_method(visibility) + klass = Class.new do + def existing_method; end + end + klass.send(visibility, :existing_method) + klass + end +end + +class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end + def test_should_allow_stubbing_existing_public_any_instance_superclass_method assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:public))) end From 6f95b2f1df66a1cf85666a642531ce31f408c10d Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 19:37:10 +0000 Subject: [PATCH 30/63] Refactor:extract stubbee_with_method to lower diff ... between test_should_allow_stubbing_existing_*_any_instance_{,super}class_method --- ...g_non_existent_any_instance_method_test.rb | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 03c4e5221..dd7e5ac0b 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -33,7 +33,7 @@ def teardown end def test_should_allow_stubbing_existing_public_any_instance_method - assert_allows_stubbing_existing_any_instance_method(class_with_method(:public)) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) end def test_should_allow_stubbing_method_to_which_any_instance_responds @@ -66,11 +66,11 @@ def respond_to?(method, _include_private = false) end def test_should_allow_stubbing_existing_protected_any_instance_method - assert_allows_stubbing_existing_any_instance_method(class_with_method(:protected)) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) end def test_should_allow_stubbing_existing_private_any_instance_method - assert_allows_stubbing_existing_any_instance_method(class_with_method(:private)) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) end def assert_allows_stubbing_existing_any_instance_method(klass) @@ -88,6 +88,10 @@ def existing_method; end klass.send(visibility, :existing_method) klass end + + def stubbee_with_method(visibility) + class_with_method(visibility) + end end class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase @@ -102,15 +106,15 @@ def teardown end def test_should_allow_stubbing_existing_public_any_instance_superclass_method - assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:public))) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) end def test_should_allow_stubbing_existing_protected_any_instance_superclass_method - assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:protected))) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) end def test_should_allow_stubbing_existing_private_any_instance_superclass_method - assert_allows_stubbing_existing_any_instance_method(Class.new(class_with_method(:private))) + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) end def assert_allows_stubbing_existing_any_instance_method(klass) @@ -128,4 +132,8 @@ def existing_method; end klass.send(visibility, :existing_method) klass end + + def stubbee_with_method(visibility) + Class.new(class_with_method(visibility)) + end end From e56097a3f46f0e736297ebbb15bf6b3a6872b915 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 19:39:05 +0000 Subject: [PATCH 31/63] rename tests to reduce diff between {,super}class --- .../stubbing_non_existent_any_instance_method_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index dd7e5ac0b..c9789e614 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -105,15 +105,15 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_existing_public_any_instance_superclass_method + def test_should_allow_stubbing_existing_public_any_instance_method assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) end - def test_should_allow_stubbing_existing_protected_any_instance_superclass_method + def test_should_allow_stubbing_existing_protected_any_instance_method assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) end - def test_should_allow_stubbing_existing_private_any_instance_superclass_method + def test_should_allow_stubbing_existing_private_any_instance_method assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) end From 6f9b6e7f286515238fee1045bd75a7ef5790e7b6 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 21:32:42 +0000 Subject: [PATCH 32/63] extract common {,super}class tests into a module --- ...g_non_existent_any_instance_method_test.rb | 93 +++++++------------ 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index c9789e614..eb969b296 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,7 +21,7 @@ def message_on_violation end end -class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase +module AllowStubbingExistingAnyInstanceMethodSharedTests include AcceptanceTest def setup @@ -36,6 +36,35 @@ def test_should_allow_stubbing_existing_public_any_instance_method assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) end + def test_should_allow_stubbing_existing_protected_any_instance_method + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) + end + + def test_should_allow_stubbing_existing_private_any_instance_method + assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) + end + + def assert_allows_stubbing_existing_any_instance_method(klass) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + test_result = run_as_test do + klass.any_instance.stubs(:existing_method) + end + assert_passed(test_result) + end + + def class_with_method(visibility) + klass = Class.new do + def existing_method; + end + end + klass.send(visibility, :existing_method) + klass + end +end + +class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase + include AllowStubbingExistingAnyInstanceMethodSharedTests + def test_should_allow_stubbing_method_to_which_any_instance_responds klass = Class.new do def respond_to?(method, _include_private = false) @@ -65,73 +94,13 @@ def respond_to?(method, _include_private = false) assert_passed(test_result) end - def test_should_allow_stubbing_existing_protected_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) - end - - def test_should_allow_stubbing_existing_private_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) - end - - def assert_allows_stubbing_existing_any_instance_method(klass) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) - end - - def class_with_method(visibility) - klass = Class.new do - def existing_method; end - end - klass.send(visibility, :existing_method) - klass - end - def stubbee_with_method(visibility) class_with_method(visibility) end end class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase - include AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_existing_public_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) - end - - def test_should_allow_stubbing_existing_protected_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) - end - - def test_should_allow_stubbing_existing_private_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) - end - - def assert_allows_stubbing_existing_any_instance_method(klass) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:existing_method) - end - assert_passed(test_result) - end - - def class_with_method(visibility) - klass = Class.new do - def existing_method; end - end - klass.send(visibility, :existing_method) - klass - end + include AllowStubbingExistingAnyInstanceMethodSharedTests def stubbee_with_method(visibility) Class.new(class_with_method(visibility)) From c58b383c743f1dcb0a7fa6cc8809ea59b9988612 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 19:57:21 +0000 Subject: [PATCH 33/63] define method programmatically than declaratively ... to allow class or superclass to be the method owner --- .../stubbing_non_existent_any_instance_method_test.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index eb969b296..060270818 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -53,10 +53,8 @@ def assert_allows_stubbing_existing_any_instance_method(klass) end def class_with_method(visibility) - klass = Class.new do - def existing_method; - end - end + klass = Class.new + klass.send(:define_method, :existing_method) {} klass.send(visibility, :existing_method) klass end From 0aef1b8fd91f7dcac76d91d64f6f12dae48176f7 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 20:03:16 +0000 Subject: [PATCH 34/63] Refactor: push stubbee creation down a method into assert_allows_stubbing_existing_any_instance_method --- .../stubbing_non_existent_any_instance_method_test.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 060270818..ec49cf79b 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -33,19 +33,20 @@ def teardown end def test_should_allow_stubbing_existing_public_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:public)) + assert_allows_stubbing_existing_any_instance_method(:public) end def test_should_allow_stubbing_existing_protected_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:protected)) + assert_allows_stubbing_existing_any_instance_method(:protected) end def test_should_allow_stubbing_existing_private_any_instance_method - assert_allows_stubbing_existing_any_instance_method(stubbee_with_method(:private)) + assert_allows_stubbing_existing_any_instance_method(:private) end - def assert_allows_stubbing_existing_any_instance_method(klass) + def assert_allows_stubbing_existing_any_instance_method(visibility) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + klass = stubbee_with_method(visibility) test_result = run_as_test do klass.any_instance.stubs(:existing_method) end From be34bf620bf01d2124784e586e8880a408777e9f Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 10 Jan 2020 21:08:15 +0000 Subject: [PATCH 35/63] allow test classes to specify method_owner,stubbee The earlier design had something like a double dispatch going on between the test classes and the shared module as follows: SharedModule#assert_allows_stubbing_existing_method -> TestClass#stubbee_with_method -> SharedModule#class_with_method. We now replace the ping-pong style with a unidirectional method call sequence: SharedModule#assert_allows_stubbing_existing_method -> TestClass#method_owner, TestClass#stubbee The new design also puts the names of the defined method and the stubbed method together in a single method body, so the correlation is more obvious and in a single location, rather than the earlier connascence of name between two methods --- ...g_non_existent_any_instance_method_test.rb | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index ec49cf79b..2da731a20 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -46,24 +46,27 @@ def test_should_allow_stubbing_existing_private_any_instance_method def assert_allows_stubbing_existing_any_instance_method(visibility) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = stubbee_with_method(visibility) + method_owner.send(:define_method, :existing_method) {} + method_owner.send(visibility, :existing_method) + stubbee_in_scope = stubbee test_result = run_as_test do - klass.any_instance.stubs(:existing_method) + stubbee_in_scope.any_instance.stubs(:existing_method) end assert_passed(test_result) end - - def class_with_method(visibility) - klass = Class.new - klass.send(:define_method, :existing_method) {} - klass.send(visibility, :existing_method) - klass - end end class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase include AllowStubbingExistingAnyInstanceMethodSharedTests + def method_owner + stubbee + end + + def stubbee + @stubbee ||= Class.new + end + def test_should_allow_stubbing_method_to_which_any_instance_responds klass = Class.new do def respond_to?(method, _include_private = false) @@ -92,16 +95,16 @@ def respond_to?(method, _include_private = false) end assert_passed(test_result) end - - def stubbee_with_method(visibility) - class_with_method(visibility) - end end class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase include AllowStubbingExistingAnyInstanceMethodSharedTests - def stubbee_with_method(visibility) - Class.new(class_with_method(visibility)) + def method_owner + stubbee.superclass + end + + def stubbee + @stubbee ||= Class.new(Class.new) end end From 55cc6b9bfc27f80d9fdf6603c1482fca6f35b1fe Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 08:43:07 +0000 Subject: [PATCH 36/63] extract stub_owner to override for class methods We were using stubbee as the class whose methods would be stubbed, and then stubbing stubbee.any_instance. However, for class method tests, we would want an object (the class) that can be stubbed by calling .stubs on it directly. We call that object the stub_owner, which in case of AnyInstance tests will be stubbee.any_instance (or, after the rename here, stubbed_instance.any_instance --- ...g_non_existent_any_instance_method_test.rb | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 2da731a20..e1484773c 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -48,9 +48,9 @@ def assert_allows_stubbing_existing_any_instance_method(visibility) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } method_owner.send(:define_method, :existing_method) {} method_owner.send(visibility, :existing_method) - stubbee_in_scope = stubbee + stub_owner_in_scope = stub_owner test_result = run_as_test do - stubbee_in_scope.any_instance.stubs(:existing_method) + stub_owner_in_scope.stubs(:existing_method) end assert_passed(test_result) end @@ -60,11 +60,15 @@ class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase include AllowStubbingExistingAnyInstanceMethodSharedTests def method_owner - stubbee + stubbed_instance end - def stubbee - @stubbee ||= Class.new + def stub_owner + stubbed_instance.any_instance + end + + def stubbed_instance + @stubbed_instance ||= Class.new end def test_should_allow_stubbing_method_to_which_any_instance_responds @@ -101,10 +105,14 @@ class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase include AllowStubbingExistingAnyInstanceMethodSharedTests def method_owner - stubbee.superclass + stubbed_instance.superclass + end + + def stub_owner + stubbed_instance.any_instance end - def stubbee - @stubbee ||= Class.new(Class.new) + def stubbed_instance + @stubbed_instance ||= Class.new(Class.new) end end From 478733c682035a180b501c0f6adcabdbf10562de Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 08:46:01 +0000 Subject: [PATCH 37/63] remove any_instance qualifier to allow use for class methods --- ...g_non_existent_any_instance_method_test.rb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index e1484773c..8d1747bef 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -21,7 +21,7 @@ def message_on_violation end end -module AllowStubbingExistingAnyInstanceMethodSharedTests +module AllowStubbingExistingMethodSharedTests include AcceptanceTest def setup @@ -32,19 +32,19 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_existing_public_any_instance_method - assert_allows_stubbing_existing_any_instance_method(:public) + def test_should_allow_stubbing_existing_public_method + assert_allows_stubbing_existing_method(:public) end - def test_should_allow_stubbing_existing_protected_any_instance_method - assert_allows_stubbing_existing_any_instance_method(:protected) + def test_should_allow_stubbing_existing_protected_method + assert_allows_stubbing_existing_method(:protected) end - def test_should_allow_stubbing_existing_private_any_instance_method - assert_allows_stubbing_existing_any_instance_method(:private) + def test_should_allow_stubbing_existing_private_method + assert_allows_stubbing_existing_method(:private) end - def assert_allows_stubbing_existing_any_instance_method(visibility) + def assert_allows_stubbing_existing_method(visibility) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } method_owner.send(:define_method, :existing_method) {} method_owner.send(visibility, :existing_method) @@ -57,7 +57,7 @@ def assert_allows_stubbing_existing_any_instance_method(visibility) end class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase - include AllowStubbingExistingAnyInstanceMethodSharedTests + include AllowStubbingExistingMethodSharedTests def method_owner stubbed_instance @@ -102,7 +102,7 @@ def respond_to?(method, _include_private = false) end class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase - include AllowStubbingExistingAnyInstanceMethodSharedTests + include AllowStubbingExistingMethodSharedTests def method_owner stubbed_instance.superclass From 702992f1f958c1b6d785352ec7c58fcccb2466db Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 08:56:57 +0000 Subject: [PATCH 38/63] move mod to own file for use in class method tests --- ...w_stubbing_existing_method_shared_tests.rb | 36 +++++++++++++++++++ ...g_non_existent_any_instance_method_test.rb | 36 +------------------ 2 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 test/acceptance/allow_stubbing_existing_method_shared_tests.rb diff --git a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb new file mode 100644 index 000000000..b734a9282 --- /dev/null +++ b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb @@ -0,0 +1,36 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) + +module AllowStubbingExistingMethodSharedTests + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end + + def test_should_allow_stubbing_existing_public_method + assert_allows_stubbing_existing_method(:public) + end + + def test_should_allow_stubbing_existing_protected_method + assert_allows_stubbing_existing_method(:protected) + end + + def test_should_allow_stubbing_existing_private_method + assert_allows_stubbing_existing_method(:private) + end + + def assert_allows_stubbing_existing_method(visibility) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + method_owner.send(:define_method, :existing_method) {} + method_owner.send(visibility, :existing_method) + stub_owner_in_scope = stub_owner + test_result = run_as_test do + stub_owner_in_scope.stubs(:existing_method) + end + assert_passed(test_result) + end +end diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 8d1747bef..0374b21aa 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,4 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -21,41 +22,6 @@ def message_on_violation end end -module AllowStubbingExistingMethodSharedTests - include AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_existing_public_method - assert_allows_stubbing_existing_method(:public) - end - - def test_should_allow_stubbing_existing_protected_method - assert_allows_stubbing_existing_method(:protected) - end - - def test_should_allow_stubbing_existing_private_method - assert_allows_stubbing_existing_method(:private) - end - - def assert_allows_stubbing_existing_method(visibility) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - method_owner.send(:define_method, :existing_method) {} - method_owner.send(visibility, :existing_method) - stub_owner_in_scope = stub_owner - test_result = run_as_test do - stub_owner_in_scope.stubs(:existing_method) - end - assert_passed(test_result) - end -end - class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase include AllowStubbingExistingMethodSharedTests From 84f2a6183f61613453008ce56e8c8e3be1db55ef Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 09:14:03 +0000 Subject: [PATCH 39/63] use existing method shared tests for class methods ... by specifying custom method_owner and stub_owner --- ...stubbing_non_existent_class_method_test.rb | 100 ++++-------------- 1 file changed, 18 insertions(+), 82 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index 341692fcd..859f5c1cf 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,4 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -19,20 +20,17 @@ def potential_violation def message_on_violation "stubbing non-existent method: #{@klass.mocha_inspect}.non_existent_method" end +end - # rubocop:disable Lint/DuplicateMethods - def test_should_allow_stubbing_existing_public_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - class << self - def existing_public_method; end - public :existing_public_method - end - end - test_result = run_as_test do - klass.stubs(:existing_public_method) - end - assert_passed(test_result) +class AllowStubbingExistingClassMethodTest < Mocha::TestCase + include AllowStubbingExistingMethodSharedTests + + def method_owner + stub_owner.singleton_class + end + + def stub_owner + @stub_owner ||= Class.new end def test_should_allow_stubbing_method_to_which_class_responds @@ -49,78 +47,16 @@ def respond_to?(method, _include_private = false) end assert_passed(test_result) end +end - def test_should_allow_stubbing_existing_protected_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - class << self - def existing_protected_method; end - protected :existing_protected_method - end - end - test_result = run_as_test do - klass.stubs(:existing_protected_method) - end - assert_passed(test_result) - end - - def test_should_allow_stubbing_existing_private_class_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - class << self - def existing_private_method; end - private :existing_private_method - end - end - test_result = run_as_test do - klass.stubs(:existing_private_method) - end - assert_passed(test_result) - end - - def test_should_allow_stubbing_existing_public_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - class << self - def existing_public_method; end - public :existing_public_method - end - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.stubs(:existing_public_method) - end - assert_passed(test_result) - end +class AllowStubbingExistingSuperclassMethodTest < Mocha::TestCase + include AllowStubbingExistingMethodSharedTests - def test_should_allow_stubbing_existing_protected_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - class << self - def existing_protected_method; end - protected :existing_protected_method - end - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.stubs(:existing_protected_method) - end - assert_passed(test_result) + def method_owner + stub_owner.superclass.singleton_class end - def test_should_allow_stubbing_existing_private_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - class << self - def existing_private_method; end - protected :existing_private_method - end - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.stubs(:existing_private_method) - end - assert_passed(test_result) + def stub_owner + @stub_owner ||= Class.new(Class.new) end - # rubocop:enable Lint/DuplicateMethods end From bac757185f8fdbf2e570ab3d66d5d6c28f5bb017 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 09:46:14 +0000 Subject: [PATCH 40/63] instance method tests use ExistingMethodSharedTest ... by specifying custom method_owner and stub_owner --- ...bbing_non_existent_instance_method_test.rb | 89 ++++--------------- 1 file changed, 18 insertions(+), 71 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index abf7eb8a4..b6014c766 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,4 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -19,18 +20,17 @@ def potential_violation def message_on_violation "stubbing non-existent method: #{@instance.mocha_inspect}.non_existent_method" end +end - def test_should_allow_stubbing_existing_public_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - def existing_public_method; end - public :existing_public_method - end - instance = klass.new - test_result = run_as_test do - instance.stubs(:existing_public_method) - end - assert_passed(test_result) +class AllowStubbingExistingInstanceMethodTest < Mocha::TestCase + include AllowStubbingExistingMethodSharedTests + + def method_owner + stub_owner.class + end + + def stub_owner + @stub_owner ||= Class.new.new end def test_should_allow_stubbing_method_to_which_instance_responds @@ -46,69 +46,16 @@ def respond_to?(method, _include_private = false) end assert_passed(test_result) end +end - def test_should_allow_stubbing_existing_protected_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - def existing_protected_method; end - protected :existing_protected_method - end - instance = klass.new - test_result = run_as_test do - instance.stubs(:existing_protected_method) - end - assert_passed(test_result) - end - - def test_should_allow_stubbing_existing_private_instance_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - def existing_private_method; end - private :existing_private_method - end - instance = klass.new - test_result = run_as_test do - instance.stubs(:existing_private_method) - end - assert_passed(test_result) - end - - def test_should_allow_stubbing_existing_public_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - def existing_public_method; end - public :existing_public_method - end - instance = Class.new(superklass).new - test_result = run_as_test do - instance.stubs(:existing_public_method) - end - assert_passed(test_result) - end +class AllowStubbingExistingInstanceSuperclassMethodTest < Mocha::TestCase + include AllowStubbingExistingMethodSharedTests - def test_should_allow_stubbing_existing_protected_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - def existing_protected_method; end - protected :existing_protected_method - end - instance = Class.new(superklass).new - test_result = run_as_test do - instance.stubs(:existing_protected_method) - end - assert_passed(test_result) + def method_owner + stub_owner.class.superclass end - def test_should_allow_stubbing_existing_private_instance_superclass_method - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - superklass = Class.new do - def existing_private_method; end - private :existing_private_method - end - instance = Class.new(superklass).new - test_result = run_as_test do - instance.stubs(:existing_private_method) - end - assert_passed(test_result) + def stub_owner + @stub_owner ||= Class.new(Class.new).new end end From 1ae84b893047544f65404749f6ce9b16e404bd76 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 12:42:48 +0000 Subject: [PATCH 41/63] pull up test_should_allow_stubbing_method_responded_to from AllowStubbingNonExistent{AnyInstance,Class,Instance}{,Superclass}MethodTest after a rename to make it more generic. This also adds the respond_to? check to superclass tests. It wasn't clear why we should check for existing methods on a class and its superclass, but skip the superclass check for a method to which we the object responds --- ...allow_stubbing_existing_method_shared_tests.rb | 12 ++++++++++++ ...bbing_non_existent_any_instance_method_test.rb | 13 ------------- .../stubbing_non_existent_class_method_test.rb | 15 --------------- .../stubbing_non_existent_instance_method_test.rb | 14 -------------- 4 files changed, 12 insertions(+), 42 deletions(-) diff --git a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb index b734a9282..611b94ada 100644 --- a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb +++ b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb @@ -23,6 +23,18 @@ def test_should_allow_stubbing_existing_private_method assert_allows_stubbing_existing_method(:private) end + def test_should_allow_stubbing_method_responded_to + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + method_owner.send(:define_method, :respond_to?) do |method| + (method == :method_responded_to) + end + stub_owner_in_scope = stub_owner + test_result = run_as_test do + stub_owner_in_scope.stubs(:method_responded_to) + end + assert_passed(test_result) + end + def assert_allows_stubbing_existing_method(visibility) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } method_owner.send(:define_method, :existing_method) {} diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 0374b21aa..16ae72501 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -37,19 +37,6 @@ def stubbed_instance @stubbed_instance ||= Class.new end - def test_should_allow_stubbing_method_to_which_any_instance_responds - klass = Class.new do - def respond_to?(method, _include_private = false) - (method == :method_to_which_instance_responds) - end - end - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - test_result = run_as_test do - klass.any_instance.stubs(:method_to_which_instance_responds) - end - assert_passed(test_result) - end - def test_should_default_to_allowing_stubbing_method_if_responds_to_depends_on_calling_initialize klass = Class.new do def initialize(attrs = {}) diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index 859f5c1cf..361f30123 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -32,21 +32,6 @@ def method_owner def stub_owner @stub_owner ||= Class.new end - - def test_should_allow_stubbing_method_to_which_class_responds - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - class << self - def respond_to?(method, _include_private = false) - (method == :method_to_which_class_responds) - end - end - end - test_result = run_as_test do - klass.stubs(:method_to_which_class_responds) - end - assert_passed(test_result) - end end class AllowStubbingExistingSuperclassMethodTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index b6014c766..2e81bb340 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -32,20 +32,6 @@ def method_owner def stub_owner @stub_owner ||= Class.new.new end - - def test_should_allow_stubbing_method_to_which_instance_responds - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - klass = Class.new do - def respond_to?(method, _include_private = false) - (method == :method_to_which_instance_responds) - end - end - instance = klass.new - test_result = run_as_test do - instance.stubs(:method_to_which_instance_responds) - end - assert_passed(test_result) - end end class AllowStubbingExistingInstanceSuperclassMethodTest < Mocha::TestCase From 2df5eedcfba13aeeae7b17428a8e19a833bce055 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 16:45:00 +0000 Subject: [PATCH 42/63] move mocha configure call to allow extraction --- .../acceptance/allow_stubbing_existing_method_shared_tests.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb index 611b94ada..4a59d01bc 100644 --- a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb +++ b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb @@ -24,10 +24,10 @@ def test_should_allow_stubbing_existing_private_method end def test_should_allow_stubbing_method_responded_to - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } method_owner.send(:define_method, :respond_to?) do |method| (method == :method_responded_to) end + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } stub_owner_in_scope = stub_owner test_result = run_as_test do stub_owner_in_scope.stubs(:method_responded_to) @@ -36,9 +36,9 @@ def test_should_allow_stubbing_method_responded_to end def assert_allows_stubbing_existing_method(visibility) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } method_owner.send(:define_method, :existing_method) {} method_owner.send(visibility, :existing_method) + Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } stub_owner_in_scope = stub_owner test_result = run_as_test do stub_owner_in_scope.stubs(:existing_method) From dae21233d50b9302edc6d98109dd9f927f04ada7 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 17:01:07 +0000 Subject: [PATCH 43/63] Refactor: extract assert_allows_stubbing_method --- .../allow_stubbing_existing_method_shared_tests.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb index 4a59d01bc..748dcf255 100644 --- a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb +++ b/test/acceptance/allow_stubbing_existing_method_shared_tests.rb @@ -27,21 +27,20 @@ def test_should_allow_stubbing_method_responded_to method_owner.send(:define_method, :respond_to?) do |method| (method == :method_responded_to) end - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } - stub_owner_in_scope = stub_owner - test_result = run_as_test do - stub_owner_in_scope.stubs(:method_responded_to) - end - assert_passed(test_result) + assert_allows_stubbing_method(:method_responded_to) end def assert_allows_stubbing_existing_method(visibility) method_owner.send(:define_method, :existing_method) {} method_owner.send(visibility, :existing_method) + assert_allows_stubbing_method(:existing_method) + end + + def assert_allows_stubbing_method(stubbed_method) Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } stub_owner_in_scope = stub_owner test_result = run_as_test do - stub_owner_in_scope.stubs(:existing_method) + stub_owner_in_scope.stubs(stubbed_method) end assert_passed(test_result) end From 79558ac0024eda8fe977a77917c8a2af070a98e6 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 16:25:47 +0000 Subject: [PATCH 44/63] rename shared test mod to move related files closer --- ...tubbing_existing_method_is_allowed_shared_tests.rb} | 2 +- .../stubbing_non_existent_any_instance_method_test.rb | 10 +++++----- .../stubbing_non_existent_class_method_test.rb | 10 +++++----- .../stubbing_non_existent_instance_method_test.rb | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) rename test/acceptance/{allow_stubbing_existing_method_shared_tests.rb => stubbing_existing_method_is_allowed_shared_tests.rb} (96%) diff --git a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb b/test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb similarity index 96% rename from test/acceptance/allow_stubbing_existing_method_shared_tests.rb rename to test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb index 748dcf255..cf423acf2 100644 --- a/test/acceptance/allow_stubbing_existing_method_shared_tests.rb +++ b/test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb @@ -1,6 +1,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__) -module AllowStubbingExistingMethodSharedTests +module StubbingExistingMethodIsAllowedSharedTests include AcceptanceTest def setup diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 16ae72501..632e6ca17 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,5 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) -require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -22,8 +22,8 @@ def message_on_violation end end -class AllowStubbingExistingAnyInstanceMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stubbed_instance @@ -54,8 +54,8 @@ def respond_to?(method, _include_private = false) end end -class AllowStubbingExistingAnyInstanceSuperclassMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingAnyInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stubbed_instance.superclass diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index 361f30123..fed91dfc9 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,5 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) -require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -22,8 +22,8 @@ def message_on_violation end end -class AllowStubbingExistingClassMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingClassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stub_owner.singleton_class @@ -34,8 +34,8 @@ def stub_owner end end -class AllowStubbingExistingSuperclassMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stub_owner.superclass.singleton_class diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 2e81bb340..ca5ab3761 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,5 +1,5 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) -require File.expand_path('../allow_stubbing_existing_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -22,8 +22,8 @@ def message_on_violation end end -class AllowStubbingExistingInstanceMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingInstanceMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stub_owner.class @@ -34,8 +34,8 @@ def stub_owner end end -class AllowStubbingExistingInstanceSuperclassMethodTest < Mocha::TestCase - include AllowStubbingExistingMethodSharedTests +class StubbingExistingInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowedSharedTests def method_owner stub_owner.class.superclass From f9c1a658802ab1611040af4b759510d0fe379f25 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 17:48:50 +0000 Subject: [PATCH 45/63] use shared tests for non-public any_instance Just as in the case of stubbing non-existent method tests, we break the single StubbingNonPublicAnyInstanceMethodTest into: - StubbingNonPublicAnyInstanceMethodTest - with just test_should_allow_stubbing_public_any_instance_method - StubbingPrivateAnyInstanceMethodTest - checks that stubbing private methods is checked (allow/warn/prevent) - StubbingProtectedAnyInstanceMethodTest - checks that stubbing private methods is checked (allow/warn/prevent) --- ...ing_non_public_any_instance_method_test.rb | 123 +++++------------- 1 file changed, 34 insertions(+), 89 deletions(-) diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 4f4cded4b..16bd9446c 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -1,5 +1,4 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase include AcceptanceTest @@ -12,117 +11,63 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } + def test_should_allow_stubbing_public_any_instance_method + Mocha.configure { |c| c.stubbing_non_public_method = :prevent } klass = Class.new do - def private_method; end - private :private_method + def public_method; end + public :public_method end test_result = run_as_test do - klass.any_instance.stubs(:private_method) + klass.any_instance.stubs(:public_method) end assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.private_method") end +end - def test_should_allow_stubbing_protected_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - klass = Class.new do - def protected_method; end - protected :protected_method - end - test_result = run_as_test do - klass.any_instance.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method") - end +class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests - def test_should_warn_when_stubbing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - klass = Class.new do + def setup + super + @klass = Class.new do def private_method; end private :private_method end - test_result = run_as_test do - klass.any_instance.stubs(:private_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.private_method") end - def test_should_warn_when_stubbing_protected_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - klass = Class.new do - def protected_method; end - protected :protected_method - end - test_result = run_as_test do - klass.any_instance.stubs(:protected_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method") + def configure_violation(config, treatment) + config.stubbing_non_public_method = treatment end - def test_should_prevent_stubbing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - def private_method; end - private :private_method - end - test_result = run_as_test do - klass.any_instance.stubs(:private_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.private_method") + def potential_violation + @klass.any_instance.stubs(:private_method) end - def test_should_prevent_stubbing_protected_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do + def message_on_violation + "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.private_method" + end +end + +class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + + def setup + super + @klass = Class.new do def protected_method; end - protected :protected_method - end - test_result = run_as_test do - klass.any_instance.stubs(:protected_method) + private :protected_method end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method") end - def test_should_default_to_allow_stubbing_private_any_instance_method - klass = Class.new do - def private_method; end - private :private_method - end - test_result = run_as_test do - klass.any_instance.stubs(:private_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.private_method") + def configure_violation(config, treatment) + config.stubbing_non_public_method = treatment end - def test_should_default_to_allow_stubbing_protected_any_instance_method - klass = Class.new do - def protected_method; end - protected :protected_method - end - test_result = run_as_test do - klass.any_instance.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method") + def potential_violation + @klass.any_instance.stubs(:protected_method) end - def test_should_allow_stubbing_public_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - def public_method; end - public :public_method - end - test_result = run_as_test do - klass.any_instance.stubs(:public_method) - end - assert_passed(test_result) + def message_on_violation + "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.protected_method" end end From 03e25ed57c0564b551c0cdd2c9b36e717d6307f5 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 17:56:15 +0000 Subject: [PATCH 46/63] reduce diff in tests for private and protected by using the same method names, and setting visibility programmatically rather than declaratively --- ...ing_non_public_any_instance_method_test.rb | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 16bd9446c..c3421f5b8 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -29,10 +29,9 @@ class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase def setup super - @klass = Class.new do - def private_method; end - private :private_method - end + @klass = Class.new + @klass.send(:define_method, :non_public_method) {} + @klass.send(:private, :non_public_method) end def configure_violation(config, treatment) @@ -40,11 +39,11 @@ def configure_violation(config, treatment) end def potential_violation - @klass.any_instance.stubs(:private_method) + @klass.any_instance.stubs(:non_public_method) end def message_on_violation - "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.private_method" + "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.non_public_method" end end @@ -53,10 +52,9 @@ class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase def setup super - @klass = Class.new do - def protected_method; end - private :protected_method - end + @klass = Class.new + @klass.send(:define_method, :non_public_method) {} + @klass.send(:protected, :non_public_method) end def configure_violation(config, treatment) @@ -64,10 +62,10 @@ def configure_violation(config, treatment) end def potential_violation - @klass.any_instance.stubs(:protected_method) + @klass.any_instance.stubs(:non_public_method) end def message_on_violation - "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.protected_method" + "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.non_public_method" end end From 19654888b0e9ba54347e384576735295c317292a Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 17:57:36 +0000 Subject: [PATCH 47/63] Refactor: extract module w/ overridden visibility --- ...ing_non_public_any_instance_method_test.rb | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index c3421f5b8..2dada37eb 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -24,14 +24,14 @@ def public_method; end end end -class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase +module StubbingNonPublicAnyInstanceMethodSharedTests include StubbingWithPotentialViolationDefaultingToAllowedSharedTests def setup super @klass = Class.new @klass.send(:define_method, :non_public_method) {} - @klass.send(:private, :non_public_method) + @klass.send(visibility, :non_public_method) end def configure_violation(config, treatment) @@ -47,25 +47,18 @@ def message_on_violation end end -class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests +class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicAnyInstanceMethodSharedTests - def setup - super - @klass = Class.new - @klass.send(:define_method, :non_public_method) {} - @klass.send(:protected, :non_public_method) - end - - def configure_violation(config, treatment) - config.stubbing_non_public_method = treatment + def visibility + :private end +end - def potential_violation - @klass.any_instance.stubs(:non_public_method) - end +class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicAnyInstanceMethodSharedTests - def message_on_violation - "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.non_public_method" + def visibility + :protected end end From c3cb5605f73d72b71cf0ce2daabad51106ea0f46 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 18:08:03 +0000 Subject: [PATCH 48/63] Refactor: extract {stub,method}_owner for override ... in StubbingNonPublic{Class,Instance}MethodTest --- ...bbing_non_public_any_instance_method_test.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 2dada37eb..b212da9e8 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -29,9 +29,8 @@ module StubbingNonPublicAnyInstanceMethodSharedTests def setup super - @klass = Class.new - @klass.send(:define_method, :non_public_method) {} - @klass.send(visibility, :non_public_method) + method_owner.send(:define_method, :non_public_method) {} + method_owner.send(visibility, :non_public_method) end def configure_violation(config, treatment) @@ -39,11 +38,19 @@ def configure_violation(config, treatment) end def potential_violation - @klass.any_instance.stubs(:non_public_method) + stub_owner.stubs(:non_public_method) end def message_on_violation - "stubbing non-public method: #{@klass.any_instance.mocha_inspect}.non_public_method" + "stubbing non-public method: #{stub_owner.mocha_inspect}.non_public_method" + end + + def stub_owner + method_owner.any_instance + end + + def method_owner + @klass ||= Class.new end end From cf22b803cb32b425dc77069c143c740c8f1f4f4a Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 20:10:42 +0000 Subject: [PATCH 49/63] extract StubbingNonPublicMethodSharedTests for use in StubbingNonPublic{Class,Instance}MethodTest with overridable method_owner and stubbed_owner later --- ...ing_non_public_any_instance_method_test.rb | 22 ++---------------- ...stubbing_non_public_method_shared_tests.rb | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 test/acceptance/stubbing_non_public_method_shared_tests.rb diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index b212da9e8..e73494645 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -1,4 +1,4 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase include AcceptanceTest @@ -25,25 +25,7 @@ def public_method; end end module StubbingNonPublicAnyInstanceMethodSharedTests - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests - - def setup - super - method_owner.send(:define_method, :non_public_method) {} - method_owner.send(visibility, :non_public_method) - end - - def configure_violation(config, treatment) - config.stubbing_non_public_method = treatment - end - - def potential_violation - stub_owner.stubs(:non_public_method) - end - - def message_on_violation - "stubbing non-public method: #{stub_owner.mocha_inspect}.non_public_method" - end + include StubbingNonPublicMethodSharedTests def stub_owner method_owner.any_instance diff --git a/test/acceptance/stubbing_non_public_method_shared_tests.rb b/test/acceptance/stubbing_non_public_method_shared_tests.rb new file mode 100644 index 000000000..66c807f9b --- /dev/null +++ b/test/acceptance/stubbing_non_public_method_shared_tests.rb @@ -0,0 +1,23 @@ +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) + +module StubbingNonPublicMethodSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + + def setup + super + method_owner.send(:define_method, :non_public_method) {} + method_owner.send(visibility, :non_public_method) + end + + def configure_violation(config, treatment) + config.stubbing_non_public_method = treatment + end + + def potential_violation + stub_owner.stubs(:non_public_method) + end + + def message_on_violation + "stubbing non-public method: #{stub_owner.mocha_inspect}.non_public_method" + end +end From 8f424f1e10549168245137484718af4fd7baf9d0 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 21:29:47 +0000 Subject: [PATCH 50/63] use shared mod in StubbingNonPublicClassMethodTest following the same structure as StubbingNonPublicAnyInstanceMethodTest - StubbingNonPublicClassMethodTest - StubbingPrivateClassMethodTest - StubbingProtectedClassMethodTest --- .../stubbing_non_public_class_method_test.rb | 151 ++++-------------- 1 file changed, 29 insertions(+), 122 deletions(-) diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index 79de8379f..d32625745 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -1,5 +1,4 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) class StubbingNonPublicClassMethodTest < Mocha::TestCase include AcceptanceTest @@ -12,125 +11,6 @@ def teardown teardown_acceptance_test end - # rubocop:disable Lint/DuplicateMethods - def test_should_allow_stubbing_private_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - klass = Class.new do - class << self - def private_method; end - private :private_method - end - end - test_result = run_as_test do - klass.stubs(:private_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.private_method") - end - - def test_should_allow_stubbing_protected_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - klass = Class.new do - class << self - def protected_method; end - protected :protected_method - end - end - test_result = run_as_test do - klass.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.protected_method") - end - - def test_should_warn_when_stubbing_private_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - klass = Class.new do - class << self - def private_method; end - private :private_method - end - end - test_result = run_as_test do - klass.stubs(:private_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.private_method") - end - - def test_should_warn_when_stubbing_protected_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - klass = Class.new do - class << self - def protected_method; end - protected :protected_method - end - end - test_result = run_as_test do - klass.stubs(:protected_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.protected_method") - end - - def test_should_prevent_stubbing_private_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - class << self - def private_method; end - private :private_method - end - end - test_result = run_as_test do - klass.stubs(:private_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.mocha_inspect}.private_method") - end - - def test_should_prevent_stubbing_protected_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - class << self - def protected_method; end - protected :protected_method - end - end - test_result = run_as_test do - klass.stubs(:protected_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.mocha_inspect}.protected_method") - end - - def test_should_default_to_allow_stubbing_private_class_method - klass = Class.new do - class << self - def private_method; end - private :private_method - end - end - test_result = run_as_test do - klass.stubs(:private_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.private_method") - end - - def test_should_default_to_allow_stubbing_protected_class_method - klass = Class.new do - class << self - def protected_method; end - protected :protected_method - end - end - test_result = run_as_test do - klass.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{klass.mocha_inspect}.protected_method") - end - def test_should_allow_stubbing_public_class_method Mocha.configure { |c| c.stubbing_non_public_method = :prevent } klass = Class.new do @@ -144,7 +24,6 @@ def public_method; end end assert_passed(test_result) end - # rubocop:enable Lint/DuplicateMethods def test_should_allow_stubbing_method_to_which_class_responds Mocha.configure { |c| c.stubbing_non_public_method = :prevent } @@ -161,3 +40,31 @@ def respond_to?(method, _include_private_methods = false) assert_passed(test_result) end end + +module StubbingNonPublicClassMethodSharedTests + include StubbingNonPublicMethodSharedTests + + def stub_owner + @stub_owner ||= Class.new + end + + def method_owner + stub_owner.singleton_class + end +end + +class StubbingPrivateClassMethodTest < Mocha::TestCase + include StubbingNonPublicClassMethodSharedTests + + def visibility + :private + end +end + +class StubbingProtectedClassMethodTest < Mocha::TestCase + include StubbingNonPublicClassMethodSharedTests + + def visibility + :protected + end +end From 39702b9d9839360162c92aafbc382dc4be223117 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Tue, 14 Jan 2020 21:37:17 +0000 Subject: [PATCH 51/63] use shared mod in StubbingNonPublicInstanceMethodTest following the same structure as StubbingNonPublic{AnyInstance,Class}MethodTest - StubbingNonPublicInstanceMethodTest - StubbingPrivateInstanceMethodTest - StubbingProtectedInstanceMethodTest --- ...tubbing_non_public_instance_method_test.rb | 133 ++++-------------- 1 file changed, 29 insertions(+), 104 deletions(-) diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index 708115cce..5eb6f40cb 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -1,5 +1,4 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) class StubbingNonPublicInstanceMethodTest < Mocha::TestCase include AcceptanceTest @@ -12,108 +11,6 @@ def teardown teardown_acceptance_test end - def test_should_allow_stubbing_private_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - instance = Class.new do - def private_method; end - private :private_method - end.new - test_result = run_as_test do - instance.stubs(:private_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.private_method") - end - - def test_should_allow_stubbing_protected_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - instance = Class.new do - def protected_method; end - protected :protected_method - end.new - test_result = run_as_test do - instance.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.protected_method") - end - - def test_should_warn_when_stubbing_private_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - instance = Class.new do - def private_method; end - private :private_method - end.new - test_result = run_as_test do - instance.stubs(:private_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.private_method") - end - - def test_should_warn_when_stubbing_protected_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - instance = Class.new do - def protected_method; end - protected :protected_method - end.new - test_result = run_as_test do - instance.stubs(:protected_method) - end - assert_passed(test_result) - assert @logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.protected_method") - end - - def test_should_prevent_stubbing_private_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - instance = Class.new do - def private_method; end - private :private_method - end.new - test_result = run_as_test do - instance.stubs(:private_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{instance.mocha_inspect}.private_method") - end - - def test_should_prevent_stubbing_protected_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - instance = Class.new do - def protected_method; end - protected :protected_method - end.new - test_result = run_as_test do - instance.stubs(:protected_method) - end - assert_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{instance.mocha_inspect}.protected_method") - end - - def test_should_default_to_allow_stubbing_private_instance_method - instance = Class.new do - def private_method; end - private :private_method - end.new - test_result = run_as_test do - instance.stubs(:private_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.private_method") - end - - def test_should_default_to_allow_stubbing_protected_instance_method - instance = Class.new do - def protected_method; end - protected :protected_method - end.new - test_result = run_as_test do - instance.stubs(:protected_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?("stubbing non-public method: #{instance.mocha_inspect}.protected_method") - end - def test_should_allow_stubbing_public_instance_method Mocha.configure { |c| c.stubbing_non_public_method = :prevent } instance = Class.new do @@ -139,3 +36,31 @@ def respond_to?(method, _include_private_methods = false) assert_passed(test_result) end end + +module StubbingNonPublicInstanceMethodSharedTests + include StubbingNonPublicMethodSharedTests + + def stub_owner + @stub_owner ||= Class.new.new + end + + def method_owner + stub_owner.class + end +end + +class StubbingPrivateInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicInstanceMethodSharedTests + + def visibility + :private + end +end + +class StubbingProtectedInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicInstanceMethodSharedTests + + def visibility + :protected + end +end From 18356c27a141f01ea654a47bdcde02140e44caf9 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 06:36:06 +0000 Subject: [PATCH 52/63] extract test allow stubbing public method to mod and use it in StubbingNonPublic{AnyInstance,Class,Instance}MethodTest. This also adds the check for stubbing method that's responded to for AnyInstance since it's more consistent with the other two and it wasn't clear why the check didn't exist for AnyInstance while it did for Class and Instance methods --- ...ing_non_public_any_instance_method_test.rb | 23 +++-------- .../stubbing_non_public_class_method_test.rb | 40 +++---------------- ...tubbing_non_public_instance_method_test.rb | 36 +++-------------- ...g_public_method_is_allowed_shared_tests.rb | 35 ++++++++++++++++ 4 files changed, 53 insertions(+), 81 deletions(-) create mode 100644 test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index e73494645..57450669f 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -1,26 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingPublicMethodIsAllowedSharedTests - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test + def stub_owner + method_owner.any_instance end - def test_should_allow_stubbing_public_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - def public_method; end - public :public_method - end - test_result = run_as_test do - klass.any_instance.stubs(:public_method) - end - assert_passed(test_result) + def method_owner + @klass ||= Class.new end end diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index d32625745..f5648b856 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -1,43 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) class StubbingNonPublicClassMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingPublicMethodIsAllowedSharedTests - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_public_class_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - class << self - def public_method; end - public :public_method - end - end - test_result = run_as_test do - klass.stubs(:public_method) - end - assert_passed(test_result) + def stub_owner + @stub_owner ||= Class.new end - def test_should_allow_stubbing_method_to_which_class_responds - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - klass = Class.new do - class << self - def respond_to?(method, _include_private_methods = false) - (method == :method_to_which_class_responds) - end - end - end - test_result = run_as_test do - klass.stubs(:method_to_which_class_responds) - end - assert_passed(test_result) + def method_owner + stub_owner.singleton_class end end diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index 5eb6f40cb..641908a8e 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -1,39 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) class StubbingNonPublicInstanceMethodTest < Mocha::TestCase - include AcceptanceTest + include StubbingPublicMethodIsAllowedSharedTests - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_public_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - instance = Class.new do - def public_method; end - public :public_method - end.new - test_result = run_as_test do - instance.stubs(:public_method) - end - assert_passed(test_result) + def stub_owner + @stub_owner ||= Class.new.new end - def test_should_allow_stubbing_method_to_which_instance_responds - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - instance = Class.new do - def respond_to?(method, _include_private_methods = false) - (method == :method_to_which_instance_responds) - end - end.new - test_result = run_as_test do - instance.stubs(:method_to_which_instance_responds) - end - assert_passed(test_result) + def method_owner + stub_owner.class end end diff --git a/test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb b/test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb new file mode 100644 index 000000000..52dfd32d8 --- /dev/null +++ b/test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb @@ -0,0 +1,35 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) + +module StubbingPublicMethodIsAllowedSharedTests + include AcceptanceTest + + def setup + setup_acceptance_test + end + + def teardown + teardown_acceptance_test + end + + def test_should_allow_stubbing_public_method + method_owner.send(:define_method, :public_method) {} + method_owner.send(:public, :public_method) + assert_allows_stubbing_method(:public_method) + end + + def test_should_allow_stubbing_method_responded_to + method_owner.send(:define_method, :respond_to?) do |method| + (method == :method_responded_to) + end + assert_allows_stubbing_method(:method_responded_to) + end + + def assert_allows_stubbing_method(stubbed_method) + Mocha.configure { |c| c.stubbing_non_public_method = :prevent } + stub_owner_in_scope = stub_owner + test_result = run_as_test do + stub_owner_in_scope.stubs(stubbed_method) + end + assert_passed(test_result) + end +end From af2f216c762355717ad630c7c4ce07ce88ad36da Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 06:50:23 +0000 Subject: [PATCH 53/63] Refactor: minor rename and reorder for consistency ... and to make duplication more obvious --- ...bing_non_existent_any_instance_method_test.rb | 8 ++------ ...ubbing_non_public_any_instance_method_test.rb | 16 ++++++++-------- .../stubbing_non_public_class_method_test.rb | 8 ++++---- .../stubbing_non_public_instance_method_test.rb | 16 ++++++++-------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 632e6ca17..6c913e342 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -26,15 +26,11 @@ class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowedSharedTests def method_owner - stubbed_instance + @method_owner ||= Class.new end def stub_owner - stubbed_instance.any_instance - end - - def stubbed_instance - @stubbed_instance ||= Class.new + method_owner.any_instance end def test_should_default_to_allowing_stubbing_method_if_responds_to_depends_on_calling_initialize diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 57450669f..c78426ef9 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -4,24 +4,24 @@ class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase include StubbingPublicMethodIsAllowedSharedTests - def stub_owner - method_owner.any_instance + def method_owner + @method_owner ||= Class.new end - def method_owner - @klass ||= Class.new + def stub_owner + method_owner.any_instance end end module StubbingNonPublicAnyInstanceMethodSharedTests include StubbingNonPublicMethodSharedTests - def stub_owner - method_owner.any_instance + def method_owner + @method_owner ||= Class.new end - def method_owner - @klass ||= Class.new + def stub_owner + method_owner.any_instance end end diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index f5648b856..b61363fd0 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -16,13 +16,13 @@ def method_owner module StubbingNonPublicClassMethodSharedTests include StubbingNonPublicMethodSharedTests - def stub_owner - @stub_owner ||= Class.new - end - def method_owner stub_owner.singleton_class end + + def stub_owner + @stub_owner ||= Class.new + end end class StubbingPrivateClassMethodTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index 641908a8e..951617b2d 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -4,25 +4,25 @@ class StubbingNonPublicInstanceMethodTest < Mocha::TestCase include StubbingPublicMethodIsAllowedSharedTests - def stub_owner - @stub_owner ||= Class.new.new - end - def method_owner stub_owner.class end -end - -module StubbingNonPublicInstanceMethodSharedTests - include StubbingNonPublicMethodSharedTests def stub_owner @stub_owner ||= Class.new.new end +end + +module StubbingNonPublicInstanceMethodSharedTests + include StubbingNonPublicMethodSharedTests def method_owner stub_owner.class end + + def stub_owner + @stub_owner ||= Class.new.new + end end class StubbingPrivateInstanceMethodTest < Mocha::TestCase From 6d3310693f6f2f642a1ab30bd80ee3dd1ef54eed Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 07:11:45 +0000 Subject: [PATCH 54/63] Refactor: extract StubbingAnyInstanceMethodHelper --- .../stubbing_any_instance_method_helper.rb | 9 +++++++++ ...g_non_existent_any_instance_method_test.rb | 10 ++-------- ...ing_non_public_any_instance_method_test.rb | 19 +++---------------- 3 files changed, 14 insertions(+), 24 deletions(-) create mode 100644 test/acceptance/stubbing_any_instance_method_helper.rb diff --git a/test/acceptance/stubbing_any_instance_method_helper.rb b/test/acceptance/stubbing_any_instance_method_helper.rb new file mode 100644 index 000000000..282fbcbe9 --- /dev/null +++ b/test/acceptance/stubbing_any_instance_method_helper.rb @@ -0,0 +1,9 @@ +module StubbingAnyInstanceMethodHelper + def method_owner + @method_owner ||= Class.new + end + + def stub_owner + method_owner.any_instance + end +end diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 6c913e342..85238b3c8 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,5 +1,6 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_any_instance_method_helper', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -24,14 +25,7 @@ def message_on_violation class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowedSharedTests - - def method_owner - @method_owner ||= Class.new - end - - def stub_owner - method_owner.any_instance - end + include StubbingAnyInstanceMethodHelper def test_should_default_to_allowing_stubbing_method_if_responds_to_depends_on_calling_initialize klass = Class.new do diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index c78426ef9..2ee024746 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -1,28 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_any_instance_method_helper', __FILE__) class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase include StubbingPublicMethodIsAllowedSharedTests - - def method_owner - @method_owner ||= Class.new - end - - def stub_owner - method_owner.any_instance - end + include StubbingAnyInstanceMethodHelper end module StubbingNonPublicAnyInstanceMethodSharedTests include StubbingNonPublicMethodSharedTests - - def method_owner - @method_owner ||= Class.new - end - - def stub_owner - method_owner.any_instance - end + include StubbingAnyInstanceMethodHelper end class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase From 6e28fd651bf3e09fa774b7d7e2d570ce7b6dc0b5 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 07:59:49 +0000 Subject: [PATCH 55/63] Refactor: extract StubbingClassMethodHelper module --- .../stubbing_class_method_helper.rb | 9 +++++++++ ...stubbing_non_existent_class_method_test.rb | 10 ++-------- .../stubbing_non_public_class_method_test.rb | 19 +++---------------- 3 files changed, 14 insertions(+), 24 deletions(-) create mode 100644 test/acceptance/stubbing_class_method_helper.rb diff --git a/test/acceptance/stubbing_class_method_helper.rb b/test/acceptance/stubbing_class_method_helper.rb new file mode 100644 index 000000000..e0ab7dbff --- /dev/null +++ b/test/acceptance/stubbing_class_method_helper.rb @@ -0,0 +1,9 @@ +module StubbingClassMethodHelper + def method_owner + stub_owner.singleton_class + end + + def stub_owner + @stub_owner ||= Class.new + end +end diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index fed91dfc9..ef956b772 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,5 +1,6 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_class_method_helper', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -24,14 +25,7 @@ def message_on_violation class StubbingExistingClassMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowedSharedTests - - def method_owner - stub_owner.singleton_class - end - - def stub_owner - @stub_owner ||= Class.new - end + include StubbingClassMethodHelper end class StubbingExistingSuperclassMethodIsAllowedTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index b61363fd0..53aabcc4a 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -1,28 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_class_method_helper', __FILE__) class StubbingNonPublicClassMethodTest < Mocha::TestCase include StubbingPublicMethodIsAllowedSharedTests - - def stub_owner - @stub_owner ||= Class.new - end - - def method_owner - stub_owner.singleton_class - end + include StubbingClassMethodHelper end module StubbingNonPublicClassMethodSharedTests include StubbingNonPublicMethodSharedTests - - def method_owner - stub_owner.singleton_class - end - - def stub_owner - @stub_owner ||= Class.new - end + include StubbingClassMethodHelper end class StubbingPrivateClassMethodTest < Mocha::TestCase From fe601c20a4200f94a12f3dffc04c5cc8b2033d27 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 08:07:38 +0000 Subject: [PATCH 56/63] Refactor: extract StubbingInstanceMethodHelper module --- .../stubbing_instance_method_helper.rb | 9 +++++++++ ...bbing_non_existent_instance_method_test.rb | 10 ++-------- ...tubbing_non_public_instance_method_test.rb | 19 +++---------------- 3 files changed, 14 insertions(+), 24 deletions(-) create mode 100644 test/acceptance/stubbing_instance_method_helper.rb diff --git a/test/acceptance/stubbing_instance_method_helper.rb b/test/acceptance/stubbing_instance_method_helper.rb new file mode 100644 index 000000000..d11c8edf6 --- /dev/null +++ b/test/acceptance/stubbing_instance_method_helper.rb @@ -0,0 +1,9 @@ +module StubbingInstanceMethodHelper + def method_owner + stub_owner.class + end + + def stub_owner + @stub_owner ||= Class.new.new + end +end diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index ca5ab3761..476e1017c 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,5 +1,6 @@ require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_instance_method_helper', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests @@ -24,14 +25,7 @@ def message_on_violation class StubbingExistingInstanceMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowedSharedTests - - def method_owner - stub_owner.class - end - - def stub_owner - @stub_owner ||= Class.new.new - end + include StubbingInstanceMethodHelper end class StubbingExistingInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index 951617b2d..aa058422a 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -1,28 +1,15 @@ require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) +require File.expand_path('../stubbing_instance_method_helper', __FILE__) class StubbingNonPublicInstanceMethodTest < Mocha::TestCase include StubbingPublicMethodIsAllowedSharedTests - - def method_owner - stub_owner.class - end - - def stub_owner - @stub_owner ||= Class.new.new - end + include StubbingInstanceMethodHelper end module StubbingNonPublicInstanceMethodSharedTests include StubbingNonPublicMethodSharedTests - - def method_owner - stub_owner.class - end - - def stub_owner - @stub_owner ||= Class.new.new - end + include StubbingInstanceMethodHelper end class StubbingPrivateInstanceMethodTest < Mocha::TestCase From 04c4a5b19b762a420110ab2c42ac73900d4a9fa6 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 07:55:23 +0000 Subject: [PATCH 57/63] Refactor: remove modules to reduce indirection --- .../stubbing_non_public_any_instance_method_test.rb | 9 +++------ test/acceptance/stubbing_non_public_class_method_test.rb | 9 +++------ .../stubbing_non_public_instance_method_test.rb | 9 +++------ 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 2ee024746..6f4737bbb 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -7,13 +7,9 @@ class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase include StubbingAnyInstanceMethodHelper end -module StubbingNonPublicAnyInstanceMethodSharedTests +class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase include StubbingNonPublicMethodSharedTests include StubbingAnyInstanceMethodHelper -end - -class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicAnyInstanceMethodSharedTests def visibility :private @@ -21,7 +17,8 @@ def visibility end class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicAnyInstanceMethodSharedTests + include StubbingNonPublicMethodSharedTests + include StubbingAnyInstanceMethodHelper def visibility :protected diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index 53aabcc4a..4d54f0a18 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -7,13 +7,9 @@ class StubbingNonPublicClassMethodTest < Mocha::TestCase include StubbingClassMethodHelper end -module StubbingNonPublicClassMethodSharedTests +class StubbingPrivateClassMethodTest < Mocha::TestCase include StubbingNonPublicMethodSharedTests include StubbingClassMethodHelper -end - -class StubbingPrivateClassMethodTest < Mocha::TestCase - include StubbingNonPublicClassMethodSharedTests def visibility :private @@ -21,7 +17,8 @@ def visibility end class StubbingProtectedClassMethodTest < Mocha::TestCase - include StubbingNonPublicClassMethodSharedTests + include StubbingNonPublicMethodSharedTests + include StubbingClassMethodHelper def visibility :protected diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index aa058422a..026ff54ca 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -7,13 +7,9 @@ class StubbingNonPublicInstanceMethodTest < Mocha::TestCase include StubbingInstanceMethodHelper end -module StubbingNonPublicInstanceMethodSharedTests +class StubbingPrivateInstanceMethodTest < Mocha::TestCase include StubbingNonPublicMethodSharedTests include StubbingInstanceMethodHelper -end - -class StubbingPrivateInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicInstanceMethodSharedTests def visibility :private @@ -21,7 +17,8 @@ def visibility end class StubbingProtectedInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicInstanceMethodSharedTests + include StubbingNonPublicMethodSharedTests + include StubbingInstanceMethodHelper def visibility :protected From b557ff8c556a00b7eac82a10bd36143fdc8ccb25 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 08:49:05 +0000 Subject: [PATCH 58/63] use StubbingXMethodHelper in StubbingNonExistentXMethodTest --- .../stubbing_non_existent_any_instance_method_test.rb | 10 +++------- .../stubbing_non_existent_class_method_test.rb | 10 +++------- .../stubbing_non_existent_instance_method_test.rb | 10 +++------- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 85238b3c8..a777d386c 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -4,22 +4,18 @@ class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests - - def setup - super - @klass = Class.new - end + include StubbingAnyInstanceMethodHelper def configure_violation(config, treatment) config.stubbing_non_existent_method = treatment end def potential_violation - @klass.any_instance.stubs(:non_existent_method) + stub_owner.stubs(:non_existent_method) end def message_on_violation - "stubbing non-existent method: #{@klass.any_instance.mocha_inspect}.non_existent_method" + "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" end end diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index ef956b772..aaefc0800 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -4,22 +4,18 @@ class StubbingNonExistentClassMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests - - def setup - super - @klass = Class.new - end + include StubbingClassMethodHelper def configure_violation(config, treatment) config.stubbing_non_existent_method = treatment end def potential_violation - @klass.stubs(:non_existent_method) + stub_owner.stubs(:non_existent_method) end def message_on_violation - "stubbing non-existent method: #{@klass.mocha_inspect}.non_existent_method" + "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" end end diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 476e1017c..45a20c4fd 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -4,22 +4,18 @@ class StubbingNonExistentInstanceMethodTest < Mocha::TestCase include StubbingWithPotentialViolationDefaultingToAllowedSharedTests - - def setup - super - @instance = Class.new.new - end + include StubbingInstanceMethodHelper def configure_violation(config, treatment) config.stubbing_non_existent_method = treatment end def potential_violation - @instance.stubs(:non_existent_method) + stub_owner.stubs(:non_existent_method) end def message_on_violation - "stubbing non-existent method: #{@instance.mocha_inspect}.non_existent_method" + "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" end end From 807758ef68beea5d1a2a91bbc5ee26acc816f3b0 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 08:54:48 +0000 Subject: [PATCH 59/63] extract StubbingNonExistentMethodSharedTests --- ...ing_non_existent_any_instance_method_test.rb | 16 ++-------------- .../stubbing_non_existent_class_method_test.rb | 16 ++-------------- ...tubbing_non_existent_instance_method_test.rb | 16 ++-------------- ...stubbing_non_existent_method_shared_tests.rb | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 42 deletions(-) create mode 100644 test/acceptance/stubbing_non_existent_method_shared_tests.rb diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index a777d386c..836db3ad5 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,22 +1,10 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) require File.expand_path('../stubbing_any_instance_method_helper', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingNonExistentMethodSharedTests include StubbingAnyInstanceMethodHelper - - def configure_violation(config, treatment) - config.stubbing_non_existent_method = treatment - end - - def potential_violation - stub_owner.stubs(:non_existent_method) - end - - def message_on_violation - "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" - end end class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index aaefc0800..af158716b 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,22 +1,10 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) require File.expand_path('../stubbing_class_method_helper', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingNonExistentMethodSharedTests include StubbingClassMethodHelper - - def configure_violation(config, treatment) - config.stubbing_non_existent_method = treatment - end - - def potential_violation - stub_owner.stubs(:non_existent_method) - end - - def message_on_violation - "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" - end end class StubbingExistingClassMethodIsAllowedTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 45a20c4fd..a68562a37 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,22 +1,10 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) require File.expand_path('../stubbing_instance_method_helper', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingNonExistentMethodSharedTests include StubbingInstanceMethodHelper - - def configure_violation(config, treatment) - config.stubbing_non_existent_method = treatment - end - - def potential_violation - stub_owner.stubs(:non_existent_method) - end - - def message_on_violation - "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" - end end class StubbingExistingInstanceMethodIsAllowedTest < Mocha::TestCase diff --git a/test/acceptance/stubbing_non_existent_method_shared_tests.rb b/test/acceptance/stubbing_non_existent_method_shared_tests.rb new file mode 100644 index 000000000..24d037ad7 --- /dev/null +++ b/test/acceptance/stubbing_non_existent_method_shared_tests.rb @@ -0,0 +1,17 @@ +require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) + +module StubbingNonExistentMethodSharedTests + include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment + end + + def potential_violation + stub_owner.stubs(:non_existent_method) + end + + def message_on_violation + "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" + end +end From 9d903366e464ccfd4676980b6adfc85340bb0950 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 09:17:20 +0000 Subject: [PATCH 60/63] Refactor: rename modules to make including tests more expressive --- ...lper.rb => stubbing_any_instance_method.rb} | 2 +- ...thod_helper.rb => stubbing_class_method.rb} | 2 +- ... => stubbing_existing_method_is_allowed.rb} | 2 +- ...d_helper.rb => stubbing_instance_method.rb} | 2 +- .../stubbing_method_unnecessarily_test.rb | 4 ++-- test/acceptance/stubbing_nil_test.rb | 4 ++-- ...ng_non_existent_any_instance_method_test.rb | 16 ++++++++-------- .../stubbing_non_existent_class_method_test.rb | 16 ++++++++-------- ...ubbing_non_existent_instance_method_test.rb | 16 ++++++++-------- ...stubbing_non_existent_method_is_checked.rb} | 6 +++--- ...bing_non_public_any_instance_method_test.rb | 18 +++++++++--------- .../stubbing_non_public_class_method_test.rb | 18 +++++++++--------- ...stubbing_non_public_instance_method_test.rb | 18 +++++++++--------- ...> stubbing_non_public_method_is_checked.rb} | 6 +++--- .../stubbing_on_non_mock_object_test.rb | 4 ++-- ...rb => stubbing_public_method_is_allowed.rb} | 2 +- ...ing_with_potential_violation_is_checked.rb} | 6 +++--- 17 files changed, 71 insertions(+), 71 deletions(-) rename test/acceptance/{stubbing_any_instance_method_helper.rb => stubbing_any_instance_method.rb} (74%) rename test/acceptance/{stubbing_class_method_helper.rb => stubbing_class_method.rb} (77%) rename test/acceptance/{stubbing_existing_method_is_allowed_shared_tests.rb => stubbing_existing_method_is_allowed.rb} (96%) rename test/acceptance/{stubbing_instance_method_helper.rb => stubbing_instance_method.rb} (75%) rename test/acceptance/{stubbing_non_existent_method_shared_tests.rb => stubbing_non_existent_method_is_checked.rb} (59%) rename test/acceptance/{stubbing_non_public_method_shared_tests.rb => stubbing_non_public_method_is_checked.rb} (68%) rename test/acceptance/{stubbing_public_method_is_allowed_shared_tests.rb => stubbing_public_method_is_allowed.rb} (94%) rename test/acceptance/{stubbing_with_potential_violation_shared_tests.rb => stubbing_with_potential_violation_is_checked.rb} (88%) diff --git a/test/acceptance/stubbing_any_instance_method_helper.rb b/test/acceptance/stubbing_any_instance_method.rb similarity index 74% rename from test/acceptance/stubbing_any_instance_method_helper.rb rename to test/acceptance/stubbing_any_instance_method.rb index 282fbcbe9..425d2bb01 100644 --- a/test/acceptance/stubbing_any_instance_method_helper.rb +++ b/test/acceptance/stubbing_any_instance_method.rb @@ -1,4 +1,4 @@ -module StubbingAnyInstanceMethodHelper +module StubbingAnyInstanceMethod def method_owner @method_owner ||= Class.new end diff --git a/test/acceptance/stubbing_class_method_helper.rb b/test/acceptance/stubbing_class_method.rb similarity index 77% rename from test/acceptance/stubbing_class_method_helper.rb rename to test/acceptance/stubbing_class_method.rb index e0ab7dbff..12667eaa8 100644 --- a/test/acceptance/stubbing_class_method_helper.rb +++ b/test/acceptance/stubbing_class_method.rb @@ -1,4 +1,4 @@ -module StubbingClassMethodHelper +module StubbingClassMethod def method_owner stub_owner.singleton_class end diff --git a/test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb b/test/acceptance/stubbing_existing_method_is_allowed.rb similarity index 96% rename from test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb rename to test/acceptance/stubbing_existing_method_is_allowed.rb index cf423acf2..9eb9a086d 100644 --- a/test/acceptance/stubbing_existing_method_is_allowed_shared_tests.rb +++ b/test/acceptance/stubbing_existing_method_is_allowed.rb @@ -1,6 +1,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__) -module StubbingExistingMethodIsAllowedSharedTests +module StubbingExistingMethodIsAllowed include AcceptanceTest def setup diff --git a/test/acceptance/stubbing_instance_method_helper.rb b/test/acceptance/stubbing_instance_method.rb similarity index 75% rename from test/acceptance/stubbing_instance_method_helper.rb rename to test/acceptance/stubbing_instance_method.rb index d11c8edf6..c709fc315 100644 --- a/test/acceptance/stubbing_instance_method_helper.rb +++ b/test/acceptance/stubbing_instance_method.rb @@ -1,4 +1,4 @@ -module StubbingInstanceMethodHelper +module StubbingInstanceMethod def method_owner stub_owner.class end diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 9cdab2a1c..d1e28d583 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -1,7 +1,7 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) class StubbingMethodUnnecessarilyTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault def configure_violation(config, treatment) config.stubbing_method_unnecessarily = treatment diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index cd09dc420..218d087cd 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -1,8 +1,8 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) if RUBY_VERSION < '2.2.0' class StubbingNilTest < Mocha::TestCase - include StubbingWithPotentialViolationSharedTests + include StubbingWithPotentialViolationIsChecked def configure_violation(config, treatment) config.stubbing_method_on_nil = treatment diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index 836db3ad5..eb5389dbd 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,15 +1,15 @@ -require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_any_instance_method_helper', __FILE__) +require File.expand_path('../stubbing_non_existent_method_is_checked', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_any_instance_method', __FILE__) class StubbingNonExistentAnyInstanceMethodTest < Mocha::TestCase - include StubbingNonExistentMethodSharedTests - include StubbingAnyInstanceMethodHelper + include StubbingNonExistentMethodIsChecked + include StubbingAnyInstanceMethod end class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests - include StubbingAnyInstanceMethodHelper + include StubbingExistingMethodIsAllowed + include StubbingAnyInstanceMethod def test_should_default_to_allowing_stubbing_method_if_responds_to_depends_on_calling_initialize klass = Class.new do @@ -29,7 +29,7 @@ def respond_to?(method, _include_private = false) end class StubbingExistingAnyInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests + include StubbingExistingMethodIsAllowed def method_owner stubbed_instance.superclass diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index af158716b..c160ec20e 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,19 +1,19 @@ -require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_class_method_helper', __FILE__) +require File.expand_path('../stubbing_non_existent_method_is_checked', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_class_method', __FILE__) class StubbingNonExistentClassMethodTest < Mocha::TestCase - include StubbingNonExistentMethodSharedTests - include StubbingClassMethodHelper + include StubbingNonExistentMethodIsChecked + include StubbingClassMethod end class StubbingExistingClassMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests - include StubbingClassMethodHelper + include StubbingExistingMethodIsAllowed + include StubbingClassMethod end class StubbingExistingSuperclassMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests + include StubbingExistingMethodIsAllowed def method_owner stub_owner.superclass.singleton_class diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index a68562a37..769d3f6c0 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,19 +1,19 @@ -require File.expand_path('../stubbing_non_existent_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_existing_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_instance_method_helper', __FILE__) +require File.expand_path('../stubbing_non_existent_method_is_checked', __FILE__) +require File.expand_path('../stubbing_existing_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_instance_method', __FILE__) class StubbingNonExistentInstanceMethodTest < Mocha::TestCase - include StubbingNonExistentMethodSharedTests - include StubbingInstanceMethodHelper + include StubbingNonExistentMethodIsChecked + include StubbingInstanceMethod end class StubbingExistingInstanceMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests - include StubbingInstanceMethodHelper + include StubbingExistingMethodIsAllowed + include StubbingInstanceMethod end class StubbingExistingInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase - include StubbingExistingMethodIsAllowedSharedTests + include StubbingExistingMethodIsAllowed def method_owner stub_owner.class.superclass diff --git a/test/acceptance/stubbing_non_existent_method_shared_tests.rb b/test/acceptance/stubbing_non_existent_method_is_checked.rb similarity index 59% rename from test/acceptance/stubbing_non_existent_method_shared_tests.rb rename to test/acceptance/stubbing_non_existent_method_is_checked.rb index 24d037ad7..0221777df 100644 --- a/test/acceptance/stubbing_non_existent_method_shared_tests.rb +++ b/test/acceptance/stubbing_non_existent_method_is_checked.rb @@ -1,7 +1,7 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) -module StubbingNonExistentMethodSharedTests - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests +module StubbingNonExistentMethodIsChecked + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault def configure_violation(config, treatment) config.stubbing_non_existent_method = treatment diff --git a/test/acceptance/stubbing_non_public_any_instance_method_test.rb b/test/acceptance/stubbing_non_public_any_instance_method_test.rb index 6f4737bbb..e3baca338 100644 --- a/test/acceptance/stubbing_non_public_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_any_instance_method_test.rb @@ -1,15 +1,15 @@ -require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_any_instance_method_helper', __FILE__) +require File.expand_path('../stubbing_non_public_method_is_checked', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_any_instance_method', __FILE__) class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase - include StubbingPublicMethodIsAllowedSharedTests - include StubbingAnyInstanceMethodHelper + include StubbingPublicMethodIsAllowed + include StubbingAnyInstanceMethod end class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingAnyInstanceMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingAnyInstanceMethod def visibility :private @@ -17,8 +17,8 @@ def visibility end class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingAnyInstanceMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingAnyInstanceMethod def visibility :protected diff --git a/test/acceptance/stubbing_non_public_class_method_test.rb b/test/acceptance/stubbing_non_public_class_method_test.rb index 4d54f0a18..c6c16106a 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -1,15 +1,15 @@ -require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_class_method_helper', __FILE__) +require File.expand_path('../stubbing_non_public_method_is_checked', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_class_method', __FILE__) class StubbingNonPublicClassMethodTest < Mocha::TestCase - include StubbingPublicMethodIsAllowedSharedTests - include StubbingClassMethodHelper + include StubbingPublicMethodIsAllowed + include StubbingClassMethod end class StubbingPrivateClassMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingClassMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingClassMethod def visibility :private @@ -17,8 +17,8 @@ def visibility end class StubbingProtectedClassMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingClassMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingClassMethod def visibility :protected diff --git a/test/acceptance/stubbing_non_public_instance_method_test.rb b/test/acceptance/stubbing_non_public_instance_method_test.rb index 026ff54ca..74fcc54aa 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -1,15 +1,15 @@ -require File.expand_path('../stubbing_non_public_method_shared_tests', __FILE__) -require File.expand_path('../stubbing_public_method_is_allowed_shared_tests', __FILE__) -require File.expand_path('../stubbing_instance_method_helper', __FILE__) +require File.expand_path('../stubbing_non_public_method_is_checked', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed', __FILE__) +require File.expand_path('../stubbing_instance_method', __FILE__) class StubbingNonPublicInstanceMethodTest < Mocha::TestCase - include StubbingPublicMethodIsAllowedSharedTests - include StubbingInstanceMethodHelper + include StubbingPublicMethodIsAllowed + include StubbingInstanceMethod end class StubbingPrivateInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingInstanceMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingInstanceMethod def visibility :private @@ -17,8 +17,8 @@ def visibility end class StubbingProtectedInstanceMethodTest < Mocha::TestCase - include StubbingNonPublicMethodSharedTests - include StubbingInstanceMethodHelper + include StubbingNonPublicMethodIsChecked + include StubbingInstanceMethod def visibility :protected diff --git a/test/acceptance/stubbing_non_public_method_shared_tests.rb b/test/acceptance/stubbing_non_public_method_is_checked.rb similarity index 68% rename from test/acceptance/stubbing_non_public_method_shared_tests.rb rename to test/acceptance/stubbing_non_public_method_is_checked.rb index 66c807f9b..f877679c9 100644 --- a/test/acceptance/stubbing_non_public_method_shared_tests.rb +++ b/test/acceptance/stubbing_non_public_method_is_checked.rb @@ -1,7 +1,7 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) -module StubbingNonPublicMethodSharedTests - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests +module StubbingNonPublicMethodIsChecked + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault def setup super diff --git a/test/acceptance/stubbing_on_non_mock_object_test.rb b/test/acceptance/stubbing_on_non_mock_object_test.rb index 2c6909c04..afd01ecba 100644 --- a/test/acceptance/stubbing_on_non_mock_object_test.rb +++ b/test/acceptance/stubbing_on_non_mock_object_test.rb @@ -1,7 +1,7 @@ -require File.expand_path('../stubbing_with_potential_violation_shared_tests', __FILE__) +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) class StubbingOnNonMockObjectTest < Mocha::TestCase - include StubbingWithPotentialViolationDefaultingToAllowedSharedTests + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault def setup super diff --git a/test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb b/test/acceptance/stubbing_public_method_is_allowed.rb similarity index 94% rename from test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb rename to test/acceptance/stubbing_public_method_is_allowed.rb index 52dfd32d8..5bc41887a 100644 --- a/test/acceptance/stubbing_public_method_is_allowed_shared_tests.rb +++ b/test/acceptance/stubbing_public_method_is_allowed.rb @@ -1,6 +1,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__) -module StubbingPublicMethodIsAllowedSharedTests +module StubbingPublicMethodIsAllowed include AcceptanceTest def setup diff --git a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb b/test/acceptance/stubbing_with_potential_violation_is_checked.rb similarity index 88% rename from test/acceptance/stubbing_with_potential_violation_shared_tests.rb rename to test/acceptance/stubbing_with_potential_violation_is_checked.rb index 1c19c8d08..4c04b9bf2 100644 --- a/test/acceptance/stubbing_with_potential_violation_shared_tests.rb +++ b/test/acceptance/stubbing_with_potential_violation_is_checked.rb @@ -1,7 +1,7 @@ require File.expand_path('../acceptance_test_helper', __FILE__) require 'mocha/configuration' -module StubbingWithPotentialViolationSharedTests +module StubbingWithPotentialViolationIsChecked include AcceptanceTest def setup @@ -38,8 +38,8 @@ def run_test_with_check(treatment = :default, &block) end end -module StubbingWithPotentialViolationDefaultingToAllowedSharedTests - include StubbingWithPotentialViolationSharedTests +module StubbingWithPotentialViolationIsCheckedAndAllowedByDefault + include StubbingWithPotentialViolationIsChecked def test_should_default_to_allow_stubbing_with_potential_violation assert_passed(stub_with_potential_violation) From d2932ce1927613ba75d0c21636387c08c2544e8c Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 15:18:44 +0000 Subject: [PATCH 61/63] Refactor: extract configure_violation to reduce diff --- test/acceptance/stubbing_existing_method_is_allowed.rb | 6 +++++- test/acceptance/stubbing_public_method_is_allowed.rb | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/acceptance/stubbing_existing_method_is_allowed.rb b/test/acceptance/stubbing_existing_method_is_allowed.rb index 9eb9a086d..c1a6cb632 100644 --- a/test/acceptance/stubbing_existing_method_is_allowed.rb +++ b/test/acceptance/stubbing_existing_method_is_allowed.rb @@ -37,11 +37,15 @@ def assert_allows_stubbing_existing_method(visibility) end def assert_allows_stubbing_method(stubbed_method) - Mocha.configure { |c| c.stubbing_non_existent_method = :prevent } + Mocha.configure { |c| configure_violation(c, :prevent) } stub_owner_in_scope = stub_owner test_result = run_as_test do stub_owner_in_scope.stubs(stubbed_method) end assert_passed(test_result) end + + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment + end end diff --git a/test/acceptance/stubbing_public_method_is_allowed.rb b/test/acceptance/stubbing_public_method_is_allowed.rb index 5bc41887a..d57374505 100644 --- a/test/acceptance/stubbing_public_method_is_allowed.rb +++ b/test/acceptance/stubbing_public_method_is_allowed.rb @@ -25,11 +25,15 @@ def test_should_allow_stubbing_method_responded_to end def assert_allows_stubbing_method(stubbed_method) - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } + Mocha.configure { |c| configure_violation(c, :prevent) } stub_owner_in_scope = stub_owner test_result = run_as_test do stub_owner_in_scope.stubs(stubbed_method) end assert_passed(test_result) end + + def configure_violation(config, treatment) + config.stubbing_non_public_method = treatment + end end From 8f6a9b494d34dcbd13e4671e9225c6a99c0c715b Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 15 Jan 2020 15:20:45 +0000 Subject: [PATCH 62/63] use StubbingPublicMethodIsAllowed in StubbingExistingMethodIsAllowed --- .../stubbing_existing_method_is_allowed.rb | 32 ++----------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/test/acceptance/stubbing_existing_method_is_allowed.rb b/test/acceptance/stubbing_existing_method_is_allowed.rb index c1a6cb632..74f8c51e0 100644 --- a/test/acceptance/stubbing_existing_method_is_allowed.rb +++ b/test/acceptance/stubbing_existing_method_is_allowed.rb @@ -1,19 +1,7 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) +require File.expand_path('../stubbing_public_method_is_allowed', __FILE__) module StubbingExistingMethodIsAllowed - include AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_existing_public_method - assert_allows_stubbing_existing_method(:public) - end + include StubbingPublicMethodIsAllowed def test_should_allow_stubbing_existing_protected_method assert_allows_stubbing_existing_method(:protected) @@ -23,28 +11,12 @@ def test_should_allow_stubbing_existing_private_method assert_allows_stubbing_existing_method(:private) end - def test_should_allow_stubbing_method_responded_to - method_owner.send(:define_method, :respond_to?) do |method| - (method == :method_responded_to) - end - assert_allows_stubbing_method(:method_responded_to) - end - def assert_allows_stubbing_existing_method(visibility) method_owner.send(:define_method, :existing_method) {} method_owner.send(visibility, :existing_method) assert_allows_stubbing_method(:existing_method) end - def assert_allows_stubbing_method(stubbed_method) - Mocha.configure { |c| configure_violation(c, :prevent) } - stub_owner_in_scope = stub_owner - test_result = run_as_test do - stub_owner_in_scope.stubs(stubbed_method) - end - assert_passed(test_result) - end - def configure_violation(config, treatment) config.stubbing_non_existent_method = treatment end From 7554c38ae771c44e2bd76da98e81095880e8c85f Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Fri, 17 Jan 2020 02:53:01 +0000 Subject: [PATCH 63/63] stubbed_instance -> callee, stub_owner -> stubbee --- test/acceptance/stubbing_any_instance_method.rb | 2 +- test/acceptance/stubbing_class_method.rb | 6 +++--- test/acceptance/stubbing_instance_method.rb | 6 +++--- .../stubbing_non_existent_any_instance_method_test.rb | 10 +++++----- .../stubbing_non_existent_class_method_test.rb | 6 +++--- .../stubbing_non_existent_instance_method_test.rb | 6 +++--- .../stubbing_non_existent_method_is_checked.rb | 4 ++-- .../stubbing_non_public_method_is_checked.rb | 4 ++-- test/acceptance/stubbing_public_method_is_allowed.rb | 4 ++-- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/test/acceptance/stubbing_any_instance_method.rb b/test/acceptance/stubbing_any_instance_method.rb index 425d2bb01..15e905ddf 100644 --- a/test/acceptance/stubbing_any_instance_method.rb +++ b/test/acceptance/stubbing_any_instance_method.rb @@ -3,7 +3,7 @@ def method_owner @method_owner ||= Class.new end - def stub_owner + def stubbee method_owner.any_instance end end diff --git a/test/acceptance/stubbing_class_method.rb b/test/acceptance/stubbing_class_method.rb index 12667eaa8..1ec5f2f4e 100644 --- a/test/acceptance/stubbing_class_method.rb +++ b/test/acceptance/stubbing_class_method.rb @@ -1,9 +1,9 @@ module StubbingClassMethod def method_owner - stub_owner.singleton_class + stubbee.singleton_class end - def stub_owner - @stub_owner ||= Class.new + def stubbee + @stubbee ||= Class.new end end diff --git a/test/acceptance/stubbing_instance_method.rb b/test/acceptance/stubbing_instance_method.rb index c709fc315..a8b24f557 100644 --- a/test/acceptance/stubbing_instance_method.rb +++ b/test/acceptance/stubbing_instance_method.rb @@ -1,9 +1,9 @@ module StubbingInstanceMethod def method_owner - stub_owner.class + stubbee.class end - def stub_owner - @stub_owner ||= Class.new.new + def stubbee + @stubbee ||= Class.new.new end end diff --git a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb index eb5389dbd..6e32cba92 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -32,14 +32,14 @@ class StubbingExistingAnyInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowed def method_owner - stubbed_instance.superclass + callee.superclass end - def stub_owner - stubbed_instance.any_instance + def stubbee + callee.any_instance end - def stubbed_instance - @stubbed_instance ||= Class.new(Class.new) + def callee + @callee ||= Class.new(Class.new) end end diff --git a/test/acceptance/stubbing_non_existent_class_method_test.rb b/test/acceptance/stubbing_non_existent_class_method_test.rb index c160ec20e..ce1687371 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -16,10 +16,10 @@ class StubbingExistingSuperclassMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowed def method_owner - stub_owner.superclass.singleton_class + stubbee.superclass.singleton_class end - def stub_owner - @stub_owner ||= Class.new(Class.new) + def stubbee + @stubbee ||= Class.new(Class.new) end end diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 769d3f6c0..cfb4d48ea 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -16,10 +16,10 @@ class StubbingExistingInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase include StubbingExistingMethodIsAllowed def method_owner - stub_owner.class.superclass + stubbee.class.superclass end - def stub_owner - @stub_owner ||= Class.new(Class.new).new + def stubbee + @stubbee ||= Class.new(Class.new).new end end diff --git a/test/acceptance/stubbing_non_existent_method_is_checked.rb b/test/acceptance/stubbing_non_existent_method_is_checked.rb index 0221777df..6c52a5f66 100644 --- a/test/acceptance/stubbing_non_existent_method_is_checked.rb +++ b/test/acceptance/stubbing_non_existent_method_is_checked.rb @@ -8,10 +8,10 @@ def configure_violation(config, treatment) end def potential_violation - stub_owner.stubs(:non_existent_method) + stubbee.stubs(:non_existent_method) end def message_on_violation - "stubbing non-existent method: #{stub_owner.mocha_inspect}.non_existent_method" + "stubbing non-existent method: #{stubbee.mocha_inspect}.non_existent_method" end end diff --git a/test/acceptance/stubbing_non_public_method_is_checked.rb b/test/acceptance/stubbing_non_public_method_is_checked.rb index f877679c9..e8b05a21d 100644 --- a/test/acceptance/stubbing_non_public_method_is_checked.rb +++ b/test/acceptance/stubbing_non_public_method_is_checked.rb @@ -14,10 +14,10 @@ def configure_violation(config, treatment) end def potential_violation - stub_owner.stubs(:non_public_method) + stubbee.stubs(:non_public_method) end def message_on_violation - "stubbing non-public method: #{stub_owner.mocha_inspect}.non_public_method" + "stubbing non-public method: #{stubbee.mocha_inspect}.non_public_method" end end diff --git a/test/acceptance/stubbing_public_method_is_allowed.rb b/test/acceptance/stubbing_public_method_is_allowed.rb index d57374505..f290aa95c 100644 --- a/test/acceptance/stubbing_public_method_is_allowed.rb +++ b/test/acceptance/stubbing_public_method_is_allowed.rb @@ -26,9 +26,9 @@ def test_should_allow_stubbing_method_responded_to def assert_allows_stubbing_method(stubbed_method) Mocha.configure { |c| configure_violation(c, :prevent) } - stub_owner_in_scope = stub_owner + stubbee_in_scope = stubbee test_result = run_as_test do - stub_owner_in_scope.stubs(stubbed_method) + stubbee_in_scope.stubs(stubbed_method) end assert_passed(test_result) end