Skip to content

Instantly share code, notes, and snippets.

@e2
Created November 21, 2014 00:13
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 e2/ba3224fb49557bb71565 to your computer and use it in GitHub Desktop.
Save e2/ba3224fb49557bb71565 to your computer and use it in GitHub Desktop.
require 'rspec'
RSpec.configure do |config|
def stub_mod(mod, excluded)
mod.constants.each do |klass_name|
klass = mod.const_get(klass_name)
if klass.is_a?(Class)
unless klass == described_class
unless excluded.include?(klass)
class_double(klass.to_s).as_stubbed_const(transfer_nested_constants: true)
end
end
elsif klass.is_a?(Module)
stub_mod(klass)
end
end
end
config.before(:each) do
stub_mod(MyMod, [MyMod::MySpecialClass])
end
end
module MyMod
class MyClass
def foo
SomethingElse.new.bar if MySpecialClass.new.baz
end
end
class SomethingElse
def bar
"If you see this, you forgot to stub me!"
end
end
class MySpecialClass
def baz
true
end
end
end
RSpec.describe MyMod::MyClass do
it "should fail because I forgot to stub SomethingElse" do
expect(subject.foo).to eq("hello")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment