Created
October 27, 2010 11:32
-
-
Save floere/648878 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
| # The trick is to just allocate a delicious object. | |
| # | |
| describe "check_gem" do | |
| before(:each) do | |
| @source = Sources::Delicious.allocate | |
| end | |
| context "doesn't find www/delicious" do | |
| before(:each) do | |
| @source.should_receive(:require). | |
| any_number_of_times.and_raise LoadError | |
| end | |
| it "puts and exits with error" do | |
| @source.should_receive(:puts).once.with "..." | |
| @source.should_receive(:exit).once.with 1 | |
| @source.check_gem | |
| end | |
| # ... more specs | |
| # To test the initialize method itself, | |
| # send the message #initialize in your | |
| # tests to the allocated object. | |
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment