Skip to content

Instantly share code, notes, and snippets.

@alloy
Forked from floere/delicious_part.rb
Created January 19, 2011 13:13
Show Gist options
  • Select an option

  • Save alloy/786153 to your computer and use it in GitHub Desktop.

Select an option

Save alloy/786153 to your computer and use it in GitHub Desktop.
# 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.
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