Contrary to the documentation on .stubs, it seems that I'm able to stub a method that doesn't exist.
Considering the following code:
class DependencyClass
def self.method_to_be_stubbed
'hello'
end
end
class TestMethodClass
def self.method_being_tested
DependencyClass.method_to_be_stubbed
end
end
class StubbedMissingMethodTest < ActiveSupport::TestCase
test '#method_being_tested should return value from stub' do
assert_equal 'hello', TestMethodClass.method_being_tested
DependencyClass.stubs(:method_to_be_stubbed).returns('goodbye')
assert_equal 'goodbye', TestMethodClass.method_being_tested
end
end
In this example, DependencyClass.stubs(:method_to_be_stubbed).returns('goodbye') works as expected because #method_to_be_stubbed exists on DependencyClass. However, if I were to change #method_to_be_stubbed to a class instance method of DependencyClass as follows:
class DependencyClass
def method_to_be_stubbed
'hello'
end
end
class StubbedMissingMethodTest < ActiveSupport::TestCase
test '#method_being_tested should return value from stub' do
assert_equal 'hello', TestMethodClass.method_being_tested
# despite the method not existing on the class,
# instead on the instance - yet it still works?
DependencyClass.stubs(:method_to_be_stubbed).returns('goodbye')
assert_equal 'goodbye', TestMethodClass.method_being_tested
end
end
My stub for #method_to_be_stubbed maintains the class method on DependencyClass, despite it not existing any longer. Is the expected behaviour not for the .stubs call to fail, since the method being stubbed does not exist?