Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider stubs on superclasses if none exist on receiver. Fixes #145.
Note: This may break existing tests which rely on the old behaviour! Stubbing a superclass method and then invoking that method on a child class would previously cause an unexpected invocation error. This was because the superclass and child class delegated to different mock objects i.e. the superclass has its own `@mocha` class instance variable which is different from the child class' `@mocha` class instance variable. By searching up through the inheritance hierarchy for each `@mocha` instance variable, we can provide a more intuitive behaviour. Instead of an unexpected invocation error (see above), invoking the method on the child class will cause the stubbed method on the superclass to be used. class Parent def self.my_class_method :original_value end end class Child < Parent end Parent.stubs(:my_class_method).returns(:stubbed_value) # old behaviour Child.my_class_method # => unexpected invocation error # new behaviour Child.my_class_method # => :stubbed_value For consistency, I have also implemented a similar change for the corresponding `any_instance` scenario: class Parent def my_instance_method :original_value end end class Child < Parent end Parent.any_instance.stubs(:my_instance_method).returns(:stubbed_value) # old behaviour Child.new.my_instance_method # => unexpected invocation error # new behaviour Child.new.my_instance_method # => :stubbed_value These changes were based in part on a suggestion by @ccutrer.
- Loading branch information
1 parent
b500ce8
commit b41254a
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
require 'mocha/setup' | ||
|
||
class StubAnyInstanceMethodDefinedOnSuperclassTest < Test::Unit::TestCase | ||
|
||
include AcceptanceTest | ||
|
||
def setup | ||
setup_acceptance_test | ||
end | ||
|
||
def teardown | ||
teardown_acceptance_test | ||
end | ||
|
||
def test_should_stub_method_and_leave_it_unchanged_after_test | ||
superklass = Class.new do | ||
def my_superclass_method | ||
:original_return_value | ||
end | ||
public :my_superclass_method | ||
end | ||
klass = Class.new(superklass) | ||
instance = klass.new | ||
assert_snapshot_unchanged(instance) do | ||
test_result = run_as_test do | ||
superklass.any_instance.stubs(:my_superclass_method).returns(:new_return_value) | ||
assert_equal :new_return_value, instance.my_superclass_method | ||
end | ||
assert_passed(test_result) | ||
end | ||
assert_equal :original_return_value, instance.my_superclass_method | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters