Created
November 15, 2022 13:47
-
-
Save jaydorsey/0ec8ea8dc15db904ede7e4cf48354c15 to your computer and use it in GitHub Desktop.
Using stub_const to hijack a constant
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
# Lines marked with pry are just for testing in a rails console | |
require 'rspec/mocks' # pry | |
include RSpec::Mocks::ExampleMethods # pry | |
# This bypasses the warning that the method isn't called inside of a test | |
RSpec::Mocks.with_temporary_scope do # pry | |
stub_const( | |
'MyClass', | |
instance_double( | |
'MyClass', | |
class_method: :foo, | |
new: instance_double( | |
'MyClass', | |
instance_method: 7 | |
) | |
) | |
) | |
puts MyClass.class_method # pry | |
puts MyClass.new.instance_method # pry | |
end # pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment