1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# frozen_string_literal: true
require File.expand_path('../acceptance_test_helper', __FILE__)
require 'mocha/configuration'
class StubbingNonPublicAnyInstanceMethodTest < Mocha::TestCase
include AcceptanceTestHelper
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_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.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 protected_method; end
protected :protected_method
end
test_result = run_as_test do
klass.any_instance.stubs(:protected_method)
end
assert_errored(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")
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
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)
end
end
|