-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stub method shared tests #458
base: main
Are you sure you want to change the base?
Changes from all commits
c203058
36888e8
c455732
d2362c5
26de50e
6fe3702
fec664a
9c47db1
90570ec
32562f1
2e425af
0d024d4
44037fe
388c608
b8505d1
60036df
e9cfc66
eef3880
a84b186
adc1d2f
bffb23d
1180f77
ba0617b
6d3b047
0ea322e
6a0bbc3
060ebb7
1b5af22
fdc2e2b
620ff24
99ca183
aad1b3f
18c8c49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,18 @@ | ||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
require File.expand_path('../stub_method_shared_tests', __FILE__) | ||
|
||
class StubAnyInstanceMethodTest < Mocha::TestCase | ||
include AcceptanceTest | ||
include StubMethodSharedTests | ||
|
||
def setup | ||
setup_acceptance_test | ||
def method_owner | ||
@method_owner ||= Class.new | ||
end | ||
|
||
def teardown | ||
teardown_acceptance_test | ||
def callee | ||
method_owner.new | ||
end | ||
|
||
def test_should_stub_public_method_within_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
end | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_instance_method, :public | ||
assert_equal :new_return_value, instance.my_instance_method | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
def test_should_leave_stubbed_public_method_unchanged_after_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
public :my_instance_method | ||
def self.public(*args); end | ||
end | ||
instance = klass.new | ||
run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
end | ||
assert(instance.public_methods(false).any? { |m| m.to_s == 'my_instance_method' }) | ||
assert_equal :original_return_value, instance.my_instance_method | ||
end | ||
|
||
def test_should_leave_stubbed_public_method_unchanged_after_test_when_it_was_originally_private_in_owning_module | ||
module_with_private_method = Module.new do | ||
def my_included_method | ||
:original_return_value | ||
end | ||
private :my_included_method | ||
end | ||
klass = Class.new do | ||
include module_with_private_method | ||
public :my_included_method | ||
end | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_included_method).returns(:new_return_value) | ||
assert_equal :new_return_value, instance.my_included_method | ||
end | ||
assert_passed(test_result) | ||
assert_equal :original_return_value, instance.my_included_method | ||
end | ||
Comment on lines
-45
to
-63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
def test_should_leave_stubbed_protected_method_unchanged_after_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
protected :my_instance_method | ||
def self.protected(*args); end | ||
|
||
def my_unprotected_instance_method | ||
my_instance_method | ||
end | ||
end | ||
instance = klass.new | ||
run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
end | ||
assert(instance.protected_methods(false).any? { |m| m.to_s == 'my_instance_method' }) | ||
assert_equal :original_return_value, instance.my_unprotected_instance_method | ||
end | ||
|
||
def test_should_stub_protected_method_within_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
protected :my_instance_method | ||
def self.protected(*args); end | ||
|
||
def my_unprotected_instance_method | ||
my_instance_method | ||
end | ||
end | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_instance_method, :protected | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
def test_should_leave_stubbed_private_method_unchanged_after_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
private :my_instance_method | ||
def self.private(*args); end | ||
end | ||
instance = klass.new | ||
run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
end | ||
assert(instance.private_methods(false).any? { |m| m.to_s == 'my_instance_method' }) | ||
assert_equal :original_return_value, instance.send(:my_instance_method) | ||
end | ||
|
||
def test_should_stub_private_method_within_test | ||
klass = Class.new do | ||
def my_instance_method | ||
:original_return_value | ||
end | ||
private :my_instance_method | ||
def self.private(*args); end | ||
end | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_instance_method, :private | ||
end | ||
assert_passed(test_result) | ||
Comment on lines
-14
to
-134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All except test_should_leave_stubbed_public_method_unchanged_after_test_when_it_was_originally_private_in_owning_module covered by https://github.com/freerange/mocha/pull/458/files#diff-2d503cd32084bef97bc2b3e3256412c7R3-R15 |
||
def stubbee | ||
method_owner.any_instance | ||
end | ||
|
||
def test_should_reset_expectations_after_test | ||
|
@@ -147,66 +28,6 @@ def my_instance_method | |
assert_equal 0, klass.any_instance.mocha.__expectations__.length | ||
end | ||
|
||
def test_should_be_able_to_stub_a_public_superclass_method | ||
superklass = Class.new do | ||
def my_superclass_method | ||
:original_return_value | ||
end | ||
public :my_superclass_method | ||
end | ||
klass = Class.new(superklass) | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_superclass_method, :public | ||
assert_equal :new_return_value, instance.my_superclass_method | ||
end | ||
assert_passed(test_result) | ||
assert(instance.public_methods(true).any? { |m| m.to_s == 'my_superclass_method' }) | ||
assert(klass.public_methods(false).none? { |m| m.to_s == 'my_superclass_method' }) | ||
assert_equal :original_return_value, instance.my_superclass_method | ||
end | ||
|
||
def test_should_be_able_to_stub_a_protected_superclass_method | ||
superklass = Class.new do | ||
def my_superclass_method | ||
:original_return_value | ||
end | ||
protected :my_superclass_method | ||
end | ||
klass = Class.new(superklass) | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_superclass_method, :protected | ||
assert_equal :new_return_value, instance.send(:my_superclass_method) | ||
end | ||
assert_passed(test_result) | ||
assert(instance.protected_methods(true).any? { |m| m.to_s == 'my_superclass_method' }) | ||
assert(klass.protected_methods(false).none? { |m| m.to_s == 'my_superclass_method' }) | ||
assert_equal :original_return_value, instance.send(:my_superclass_method) | ||
end | ||
|
||
def test_should_be_able_to_stub_a_private_superclass_method | ||
superklass = Class.new do | ||
def my_superclass_method | ||
:original_return_value | ||
end | ||
private :my_superclass_method | ||
end | ||
klass = Class.new(superklass) | ||
instance = klass.new | ||
test_result = run_as_test do | ||
klass.any_instance.stubs(:my_superclass_method).returns(:new_return_value) | ||
assert_method_visibility instance, :my_superclass_method, :private | ||
assert_equal :new_return_value, instance.send(:my_superclass_method) | ||
end | ||
assert_passed(test_result) | ||
assert(instance.private_methods(true).any? { |m| m.to_s == 'my_superclass_method' }) | ||
assert(klass.private_methods(false).none? { |m| m.to_s == 'my_superclass_method' }) | ||
assert_equal :original_return_value, instance.send(:my_superclass_method) | ||
end | ||
Comment on lines
-150
to
-208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
# rubocop:disable Lint/DuplicateMethods | ||
def test_should_be_able_to_stub_method_if_ruby18_public_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy | ||
ruby18_klass = Class.new do | ||
|
@@ -299,3 +120,26 @@ def private_instance_methods(_include_superclass = true) | |
end | ||
# rubocop:enable Lint/DuplicateMethods | ||
end | ||
|
||
class StubAnyInstanceMethodOriginallyPrivateInOwningModuleTest < Mocha::TestCase | ||
include StubMethodSharedTests | ||
|
||
def method_owner | ||
@method_owner ||= Class.new.send(:include, module_with_private_method) | ||
end | ||
|
||
def callee | ||
method_owner.new | ||
end | ||
|
||
def stubbee | ||
method_owner.any_instance | ||
end | ||
|
||
def module_with_private_method | ||
mod = Module.new | ||
mod.send(:define_method, stubbed_method_name) { :private_module_method_return_value } | ||
mod.send(:private, stubbed_method_name) | ||
mod | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Covered by https://github.com/freerange/mocha/pull/458/files#diff-3a2ccaea62f0fe445b2c85036067897aR51-R65