Skip to content

Commit

Permalink
instance method tests use ExistingMethodSharedTest
Browse files Browse the repository at this point in the history
... by specifying custom method_owner and stub_owner
  • Loading branch information
nitishr committed Jan 16, 2020
1 parent 84f2a61 commit bac7571
Showing 1 changed file with 18 additions and 71 deletions.
89 changes: 18 additions & 71 deletions test/acceptance/stubbing_non_existent_instance_method_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit bac7571

Please sign in to comment.