-
Notifications
You must be signed in to change notification settings - Fork 20
Description
When mocking module_functions / extend self modules, Spy does not recognize the ownership of the original method and won't re-apply it to the module during teardown. Here is a quick Minitest that shows this happening:
require 'test_helper'
class SpyTesting < ActiveSupport::TestCase
module Example
extend self
def hello
'hello world'
end
end
test 'one' do
Spy.on(Example, :hello).and_return('yo')
assert_equal Example.hello, 'yo'
end
test 'two' do
Spy.on(Example, :hello).and_return('yo')
assert_equal Example.hello, 'yo'
end
end
Running this code will always have one of the two tests fail.
Error:
SpyTesting#test_two:
NameError: undefined method `hello' for class `#<Class:SpyTesting::Example>'
test/spy_testing.rb:18:in `block in <class:SpyTesting>'
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
This is due to the fact that in lib/spy/subroutine.rb:87 the method_owner does not equal the original_method.owner.
method_owner
=> #<Class:SpyTesting::Example>
method_owner.class
=> Class
original_method.owner
=> SpyTesting::Example
original_method.owner.class
=> Module
I'm still looking for the easiest fix, but. I figure I should share the issue either way. This works without issue in spy 1.0.0, which has been our solution for a while, but we will need to go to ruby 3 eventually. Removing the second conditional to the if statement will cause the tests to pass, but I assume it's there for an excellent reason. I'm still learning the internals.