-
-
Save alloy/786153 to your computer and use it in GitHub Desktop.
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
| # The problem: | |
| # I wanted to test check_gem, which needs to be called | |
| # in the initializer (well, *I* need it to be called in | |
| # the initializer) | |
| # | |
| class Delicious < Base | |
| def initialize username, password | |
| check_gem | |
| # ... | |
| end | |
| def check_gem | |
| require 'www/delicious' | |
| rescue LoadError | |
| puts "..." # Gem missing message omitted. | |
| exit 1 | |
| end | |
| # ... | |
| # I could of course initialize an instance, stub | |
| # require and call check_gem again. But this is | |
| # not the way to go. |
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
| class DeliciousMock < Delicious | |
| def received_calls | |
| @received_calls ||= [] | |
| end | |
| def require(name) | |
| raise LoadError | |
| end | |
| def puts(*args) | |
| received_calls << [:puts, args] | |
| end | |
| def exit(code) | |
| received_calls << [:exit, code] | |
| end | |
| end | |
| describe "check_gem" do | |
| before(:each) do | |
| @source = DeliciousMock.new | |
| end | |
| context "doesn't find www/delicious" do | |
| it "puts and exits with error" do | |
| @source.received_calls.should include([:puts, "..."]) | |
| @source.received_calls.should include([:exit, 1]) | |
| end | |
| # ... more specs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment