Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Forked from justinko/gist:1509034
Created December 22, 2011 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save myronmarston/1509315 to your computer and use it in GitHub Desktop.
Save myronmarston/1509315 to your computer and use it in GitHub Desktop.
Avdi Grimm's creation, pulled from Objects on Rails
module ModuleStubbing
def stubbed_modules
@stubbed_modules ||= []
end
def stub_module(full_name)
most_shallow_stubbed_module = nil
full_name.to_s.split(/::/).inject(Object) do |context, name|
begin
context.const_get(name)
rescue NameError
most_shallow_stubbed_module ||= [context, name]
context.const_set(name, Module.new)
end
end.tap { stubbed_modules << most_shallow_stubbed_module }
end
def cleanup_stub_modules
stubbed_modules.each do |(context, name)|
context.send(:remove_const, name)
end
end
end
RSpec.configure do |c|
c.include ModuleStubbing
c.after(:each) { cleanup_stub_modules }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment