-
-
Save justinko/1511607 to your computer and use it in GitHub Desktop.
Avdi Grimm's creation, pulled from Objects on Rails
This file contains hidden or 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
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 do | |
if most_shallow_stubbed_module | |
stubbed_modules << most_shallow_stubbed_module | |
end | |
end | |
end | |
def cleanup_stub_modules | |
stubbed_modules.each do |(context, name)| | |
context.send(:remove_const, name) | |
end | |
end | |
end | |
include ModuleStubbing | |
RSpec.configure do |c| | |
c.after(:each) { cleanup_stub_modules } | |
end |
A simple include Bar
in Foo
will require you to use stub_module('Bar')
in the top level. One way to avoid stub_module
is to include
it like so:
class Foo
include Bar if defined?(Bar)
end
But then you're adding test specific logic into the implementation code :(
maybe these things are very coupled and testing it in full isolation may not make sense
Not going to be an option. Currently, this suite takes 11 minutes to run. My goal is to have the capybara specs be the only "full stack" specs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thought: this is only necessary if you use
Bar
directly inFoo
's class body, right? If you only useBar
inFoo
's methods, but not directly in the class body, you wouldn't need to make a stub module before requiring foo. Alternately, full isolation is nice, but if it's important for to useBar
directly in the class body ofFoo
maybe these things are very coupled and testing it in full isolation may not make sense.