Created
May 7, 2016 03:36
-
-
Save mclosson/11906ef19494a5adcb9073ddeac77255 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
require 'httparty' | |
require 'minitest/autorun' | |
require 'minitest/mock' | |
require 'httparty' | |
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