Skip to content

Instantly share code, notes, and snippets.

@kbaribeau
Created October 19, 2011 01:34
Show Gist options
  • Save kbaribeau/1297273 to your computer and use it in GitHub Desktop.
Save kbaribeau/1297273 to your computer and use it in GitHub Desktop.
explicit requires, fast specs, and crazy dependencies
#foo.rb
class Foo < Bar
end
#bar.rb
class Bar < ActiveRecord::Base
#all sorts of crazy code that depends on a bunch of wacky stuff my tests don't care about
end
#In my test code, I want to do this
it "does something" do
fake_foo = stub
Foo.stub(:find).and_return(fake_foo) #note that referencing Foo requires the class Foo to be defined
end
#but, I can't require foo.rb because I would need to require bar.rb, which requires me to pull in a bunch of wacky stuff
#my current solution is something like this:
before {
class Foo; end
}
it "does something" do
Foo.stub(:and).and_return(fake_foo)
end
after {
Object.send(:remove_const, "Foo")
}
#But that seems ugly. I'm looking for better ideas. Thoughts?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment