diff --git a/test/acceptance/stubbing_any_instance_method.rb b/test/acceptance/stubbing_any_instance_method.rb new file mode 100644 index 000000000..15e905ddf --- /dev/null +++ b/test/acceptance/stubbing_any_instance_method.rb @@ -0,0 +1,9 @@ +module StubbingAnyInstanceMethod + def method_owner + @method_owner ||= Class.new + end + + def stubbee + method_owner.any_instance + end +end diff --git a/test/acceptance/stubbing_class_method.rb b/test/acceptance/stubbing_class_method.rb new file mode 100644 index 000000000..1ec5f2f4e --- /dev/null +++ b/test/acceptance/stubbing_class_method.rb @@ -0,0 +1,9 @@ +module StubbingClassMethod + def method_owner + stubbee.singleton_class + end + + def stubbee + @stubbee ||= Class.new + end +end diff --git a/test/acceptance/stubbing_existing_method_is_allowed.rb b/test/acceptance/stubbing_existing_method_is_allowed.rb new file mode 100644 index 000000000..74f8c51e0 --- /dev/null +++ b/test/acceptance/stubbing_existing_method_is_allowed.rb @@ -0,0 +1,23 @@ +require File.expand_path('../stubbing_public_method_is_allowed', __FILE__) + +module StubbingExistingMethodIsAllowed + include StubbingPublicMethodIsAllowed + + 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) + method_owner.send(:define_method, :existing_method) {} + method_owner.send(visibility, :existing_method) + assert_allows_stubbing_method(:existing_method) + end + + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment + end +end diff --git a/test/acceptance/stubbing_instance_method.rb b/test/acceptance/stubbing_instance_method.rb new file mode 100644 index 000000000..a8b24f557 --- /dev/null +++ b/test/acceptance/stubbing_instance_method.rb @@ -0,0 +1,9 @@ +module StubbingInstanceMethod + def method_owner + stubbee.class + end + + def stubbee + @stubbee ||= Class.new.new + end +end diff --git a/test/acceptance/stubbing_method_unnecessarily_test.rb b/test/acceptance/stubbing_method_unnecessarily_test.rb index 12ef4fa8f..d1e28d583 100644 --- a/test/acceptance/stubbing_method_unnecessarily_test.rb +++ b/test/acceptance/stubbing_method_unnecessarily_test.rb @@ -1,59 +1,23 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) class StubbingMethodUnnecessarilyTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault - def setup - setup_acceptance_test + def configure_violation(config, treatment) + config.stubbing_method_unnecessarily = treatment end - def teardown - teardown_acceptance_test + def potential_violation + mock = mock('mock') + mock.stubs(:public_method) 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 - 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 - 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 - assert_failed(test_result) - assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method unnecessarily: #.public_method(any_parameters)') - end - - def test_should_default_to_allow_stubbing_method_unnecessarily - test_result = run_as_test do - mock = mock('mock') - mock.stubs(:public_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?('stubbing method unnecessarily: #.public_method(any_parameters)') + def message_on_violation + 'stubbing method unnecessarily: #.public_method(any_parameters)' 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 diff --git a/test/acceptance/stubbing_nil_test.rb b/test/acceptance/stubbing_nil_test.rb index 361083d2e..218d087cd 100644 --- a/test/acceptance/stubbing_nil_test.rb +++ b/test/acceptance/stubbing_nil_test.rb @@ -1,58 +1,30 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) -class StubbingNilTest < Mocha::TestCase - include AcceptanceTest +if RUBY_VERSION < '2.2.0' + class StubbingNilTest < Mocha::TestCase + include StubbingWithPotentialViolationIsChecked - def setup - setup_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 - nil.stubs(:stubbed_method) - end - assert_passed(test_result) - assert !@logger.warnings.include?('stubbing method on nil: nil.stubbed_method') + def configure_violation(config, treatment) + config.stubbing_method_on_nil = treatment 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') + def potential_violation + nil.stubs(:stubbed_method) 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 - assert_failed(test_result) - assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method on nil: nil.stubbed_method') + def message_on_violation + 'stubbing method on nil: nil.stubbed_method' end - def test_should_default_to_prevent_stubbing_method_on_non_mock_object - test_result = run_as_test do - nil.stubs(: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: stubbing method on nil: nil.stubbed_method') + assert test_result.error_messages.include?("Mocha::StubbingError: #{message_on_violation}") end 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) + test_result = run_test_with_check(:prevent) do + Object.new.stubs(:stubbed_method) end assert_passed(test_result) 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 fe518cc37..6e32cba92 100644 --- a/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_any_instance_method_test.rb @@ -1,80 +1,15 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - 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) - 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") - 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 !@logger.warnings.include?("stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method") - assert_passed(test_result) - end + include StubbingNonExistentMethodIsChecked + include StubbingAnyInstanceMethod +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_public_method; end - public :existing_public_method - end - test_result = run_as_test do - klass.any_instance.stubs(:existing_public_method) - end - assert_passed(test_result) - 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 - test_result = run_as_test do - klass.any_instance.stubs(:method_to_which_instance_responds) - end - assert_passed(test_result) - end +class StubbingExistingAnyInstanceMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed + include StubbingAnyInstanceMethod def test_should_default_to_allowing_stubbing_method_if_responds_to_depends_on_calling_initialize klass = Class.new do @@ -91,67 +26,20 @@ def respond_to?(method, _include_private = false) end assert_passed(test_result) end +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_protected_method; end - protected :existing_protected_method - end - test_result = run_as_test do - klass.any_instance.stubs(:existing_protected_method) - end - assert_passed(test_result) - 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 - end - test_result = run_as_test do - klass.any_instance.stubs(:existing_private_method) - end - assert_passed(test_result) - end +class StubbingExistingAnyInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed - 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 - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.any_instance.stubs(:existing_public_method) - end - assert_passed(test_result) + def method_owner + callee.superclass 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 - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.any_instance.stubs(:existing_protected_method) - end - assert_passed(test_result) + def stubbee + callee.any_instance 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 - end - klass = Class.new(superklass) - test_result = run_as_test do - klass.any_instance.stubs(:existing_private_method) - end - assert_passed(test_result) + 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 4c9ad538e..ce1687371 100644 --- a/test/acceptance/stubbing_non_existent_class_method_test.rb +++ b/test/acceptance/stubbing_non_existent_class_method_test.rb @@ -1,157 +1,25 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - 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") - 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") - 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) - 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) - 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 - - 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 + include StubbingNonExistentMethodIsChecked + include StubbingClassMethod +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 +class StubbingExistingClassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed + include StubbingClassMethod +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 StubbingExistingSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed - 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 + stubbee.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 stubbee + @stubbee ||= Class.new(Class.new) end - # rubocop:enable Lint/DuplicateMethods end diff --git a/test/acceptance/stubbing_non_existent_instance_method_test.rb b/test/acceptance/stubbing_non_existent_instance_method_test.rb index 8ce7841ed..cfb4d48ea 100644 --- a/test/acceptance/stubbing_non_existent_instance_method_test.rb +++ b/test/acceptance/stubbing_non_existent_instance_method_test.rb @@ -1,145 +1,25 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - 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") - 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") - 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) - 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) - 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 - - 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 + include StubbingNonExistentMethodIsChecked + include StubbingInstanceMethod +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 +class StubbingExistingInstanceMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed + include StubbingInstanceMethod +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 StubbingExistingInstanceSuperclassMethodIsAllowedTest < Mocha::TestCase + include StubbingExistingMethodIsAllowed - 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 + stubbee.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 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 new file mode 100644 index 000000000..6c52a5f66 --- /dev/null +++ b/test/acceptance/stubbing_non_existent_method_is_checked.rb @@ -0,0 +1,17 @@ +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) + +module StubbingNonExistentMethodIsChecked + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault + + def configure_violation(config, treatment) + config.stubbing_non_existent_method = treatment + end + + def potential_violation + stubbee.stubs(:non_existent_method) + end + + def message_on_violation + "stubbing non-existent method: #{stubbee.mocha_inspect}.non_existent_method" + end +end 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..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,128 +1,26 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - def teardown - teardown_acceptance_test - end - - def test_should_allow_stubbing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :allow } - 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_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 - - def test_should_warn_when_stubbing_private_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :warn } - 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") - 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") - end + include StubbingPublicMethodIsAllowed + include StubbingAnyInstanceMethod +end - def test_should_prevent_stubbing_protected_any_instance_method - Mocha.configure { |c| c.stubbing_non_public_method = :prevent } - 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_failed(test_result) - assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method") - end +class StubbingPrivateAnyInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingAnyInstanceMethod - 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 visibility + :private end +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") - end +class StubbingProtectedAnyInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingAnyInstanceMethod - 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 visibility + :protected 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 79de8379f..c6c16106a 100644 --- a/test/acceptance/stubbing_non_public_class_method_test.rb +++ b/test/acceptance/stubbing_non_public_class_method_test.rb @@ -1,163 +1,26 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - 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 + include StubbingPublicMethodIsAllowed + include StubbingClassMethod +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 +class StubbingPrivateClassMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingClassMethod - 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") + def visibility + :private end +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) - end - # rubocop:enable Lint/DuplicateMethods +class StubbingProtectedClassMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingClassMethod - 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 visibility + :protected 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 708115cce..74fcc54aa 100644 --- a/test/acceptance/stubbing_non_public_instance_method_test.rb +++ b/test/acceptance/stubbing_non_public_instance_method_test.rb @@ -1,141 +1,26 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +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 AcceptanceTest - - def setup - setup_acceptance_test - end - - 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 + include StubbingPublicMethodIsAllowed + include StubbingInstanceMethod +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 +class StubbingPrivateInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingInstanceMethod - 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") + def visibility + :private end +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) - end +class StubbingProtectedInstanceMethodTest < Mocha::TestCase + include StubbingNonPublicMethodIsChecked + include StubbingInstanceMethod - 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 visibility + :protected end end diff --git a/test/acceptance/stubbing_non_public_method_is_checked.rb b/test/acceptance/stubbing_non_public_method_is_checked.rb new file mode 100644 index 000000000..e8b05a21d --- /dev/null +++ b/test/acceptance/stubbing_non_public_method_is_checked.rb @@ -0,0 +1,23 @@ +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) + +module StubbingNonPublicMethodIsChecked + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault + + 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 + stubbee.stubs(:non_public_method) + end + + def message_on_violation + "stubbing non-public method: #{stubbee.mocha_inspect}.non_public_method" + end +end diff --git a/test/acceptance/stubbing_on_non_mock_object_test.rb b/test/acceptance/stubbing_on_non_mock_object_test.rb index d1a08d2a7..afd01ecba 100644 --- a/test/acceptance/stubbing_on_non_mock_object_test.rb +++ b/test/acceptance/stubbing_on_non_mock_object_test.rb @@ -1,62 +1,25 @@ -require File.expand_path('../acceptance_test_helper', __FILE__) -require 'mocha/configuration' +require File.expand_path('../stubbing_with_potential_violation_is_checked', __FILE__) class StubbingOnNonMockObjectTest < Mocha::TestCase - include AcceptanceTest + include StubbingWithPotentialViolationIsCheckedAndAllowedByDefault 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 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") + def message_on_violation + "stubbing method on non-mock object: #{@non_mock_object.mocha_inspect}.existing_method" end def test_should_allow_stubbing_method_on_mock_object diff --git a/test/acceptance/stubbing_public_method_is_allowed.rb b/test/acceptance/stubbing_public_method_is_allowed.rb new file mode 100644 index 000000000..f290aa95c --- /dev/null +++ b/test/acceptance/stubbing_public_method_is_allowed.rb @@ -0,0 +1,39 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) + +module StubbingPublicMethodIsAllowed + 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| configure_violation(c, :prevent) } + stubbee_in_scope = stubbee + test_result = run_as_test do + stubbee_in_scope.stubs(stubbed_method) + end + assert_passed(test_result) + end + + def configure_violation(config, treatment) + config.stubbing_non_public_method = treatment + end +end diff --git a/test/acceptance/stubbing_with_potential_violation_is_checked.rb b/test/acceptance/stubbing_with_potential_violation_is_checked.rb new file mode 100644 index 000000000..4c04b9bf2 --- /dev/null +++ b/test/acceptance/stubbing_with_potential_violation_is_checked.rb @@ -0,0 +1,48 @@ +require File.expand_path('../acceptance_test_helper', __FILE__) +require 'mocha/configuration' + +module StubbingWithPotentialViolationIsChecked + 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?(message_on_violation) + end + + def test_should_warn_on_stubbing_with_potential_violation + assert_passed(stub_with_potential_violation(:warn)) + 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: #{message_on_violation}") + 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 + +module StubbingWithPotentialViolationIsCheckedAndAllowedByDefault + include StubbingWithPotentialViolationIsChecked + + 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