Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Last active August 29, 2015 14:06
Show Gist options
  • Save jcasimir/2d816e99c0a5932a6f63 to your computer and use it in GitHub Desktop.
Save jcasimir/2d816e99c0a5932a6f63 to your computer and use it in GitHub Desktop.
Gets.chomp stubbing with Dependency Injection
class InputFetcher
def fetch
gets.chomp
end
end
class Game
attr_reader :fetcher
def initialize(fetcher_type = InputFetcher)
@fetcher = fetcher_type.new
end
def play
unless fetcher.fetch == "quit"
puts "Still Going"
end
return "done"
end
end
class GameTest < Minitest::Test
class FakeFetcher
def fetch
"quit"
end
end
def test_it_exits
game = Game.new(FakeFetcher)
assert_equal "done", game.play
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment