Please sign in to comment.
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...
Showing with 73 additions and 4 deletions.
|@@ -0,0 +1,34 @@|
|+require File.expand_path('../acceptance_test_helper', __FILE__)|
|+class StubAnyInstanceMethodDefinedOnSuperclassTest < Test::Unit::TestCase|
|+ include AcceptanceTest|
|+ def setup|
|+ def teardown|
|+ def test_should_stub_method_and_leave_it_unchanged_after_test|
|+ superklass = Class.new do|
|+ def my_superclass_method|
|+ public :my_superclass_method|
|+ klass = Class.new(superklass)|
|+ instance = klass.new|
|+ assert_snapshot_unchanged(instance) do|
|+ test_result = run_as_test do|
|+ assert_equal :new_return_value, instance.my_superclass_method|
|+ assert_equal :original_return_value, instance.my_superclass_method|