Skip to content

Instantly share code, notes, and snippets.

@mclosson
Last active May 7, 2016 04:17
Show Gist options
  • Save mclosson/10cb4399773aea0ea27374a5400f37e3 to your computer and use it in GitHub Desktop.
Save mclosson/10cb4399773aea0ea27374a5400f37e3 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'minitest/autorun'
require 'minitest/mock'
class GoogleClient
BASE_URI = "http://google.com"
attr_writer :http_client
def initialize(http_client = nil)
@http_client = http_client || HTTParty
end
def query(query_string)
http_client.get(BASE_URI + '/search?q=' + query_string.tr(" ", "+"))
end
private
def http_client
@http_client
end
end
class GoogleClientTest < Minitest::Test
def test_calls_http_client_using_stub
mock = Minitest::Mock.new
mock.expect(:get, nil, ["http://google.com/search?q=cats"])
client = GoogleClient.new
client.stub(:http_client, mock) do
client.query('cats')
end
mock.verify
end
def test_calls_http_client_using_constructor_injection
mock = Minitest::Mock.new
mock.expect(:get, nil, ["http://google.com/search?q=cats"])
client = GoogleClient.new(mock)
client.query('cats')
mock.verify
end
def test_calls_http_client_using_setter_injection
mock = Minitest::Mock.new
mock.expect(:get, nil, ["http://google.com/search?q=cats"])
client = GoogleClient.new
client.http_client = mock
client.query('cats')
mock.verify
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment