Skip to content

Commit

Permalink
Improve URL testing
Browse files Browse the repository at this point in the history
Please, don't test private methods via `send`.

Use `Minitest::Mock`.
  • Loading branch information
AlexWayfer committed Jan 25, 2018
1 parent ed73b16 commit 1c9bfca
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions test/timezone/lookup/test_google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,48 @@ def test_google_request_denied_read_lat_long_coordinates

def test_url_non_enterprise
Timecop.freeze(Time.at(1_433_347_661)) do
result = lookup.send(:url, '123', '123')
mine = lookup('{ "status": "OK" }')

params = {
'location' => '123%2C123',
'timestamp' => '1433347661',
'key' => 'MTIzYWJj'
}.map { |k, v| "#{k}=#{v}" }

assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
url_method = Minitest::Mock.new
url_method.expect(
:call, "/maps/api/timezone/json?#{params.join('&')}", %w[123 123]
)

mine.stub :url, url_method do
mine.lookup('123', '123')
end

url_method.verify
end
end

def test_url_enterprise
mine = lookup { |c| c.client_id = '123&asdf' }
mine = lookup('{ "status": "OK" }') { |c| c.client_id = '123&asdf' }

Timecop.freeze(Time.at(1_433_347_661)) do
result = mine.send(:url, '123', '123')
params = {
'location' => '123%2C123',
'timestamp' => '1433347661',
'client' => '123%26asdf',
'signature' => 'B1TNSSvIw9Wvf_ZjjW5uRzGm4F4='
}.map { |k, v| "#{k}=#{v}" }

assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
url_method = Minitest::Mock.new
url_method.expect(
:call, "/maps/api/timezone/json?#{params.join('&')}", %w[123 123]
)

mine.stub :url, url_method do
mine.lookup('123', '123')
end

url_method.verify
end
end

Expand Down

0 comments on commit 1c9bfca

Please sign in to comment.