Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Created November 15, 2022 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydorsey/0ec8ea8dc15db904ede7e4cf48354c15 to your computer and use it in GitHub Desktop.
Save jaydorsey/0ec8ea8dc15db904ede7e4cf48354c15 to your computer and use it in GitHub Desktop.
Using stub_const to hijack a constant
# 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