From 41d05a6bf0f66311b627b0ec3bba8c777223c71f Mon Sep 17 00:00:00 2001 From: Xavier MALPARTY Date: Wed, 30 Jun 2021 18:41:59 +0700 Subject: [PATCH] [#7] Wrap specs with describe #method_name --- spec/services/google/client_service_spec.rb | 40 ++++++------- spec/services/google/parser_service_spec.rb | 62 +++++++++++---------- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/spec/services/google/client_service_spec.rb b/spec/services/google/client_service_spec.rb index bb1c8491..8f386cbd 100644 --- a/spec/services/google/client_service_spec.rb +++ b/spec/services/google/client_service_spec.rb @@ -3,34 +3,36 @@ require 'rails_helper' RSpec.describe Google::ClientService, type: :service do - context 'when querying a simple keyword' do - it 'returns an HTTParty Response', vcr: 'google_search/base' do - result = described_class.new(keyword: FFaker::Lorem.word).call + describe '#call' do + context 'when querying a simple keyword' do + it 'returns an HTTParty Response', vcr: 'google_search/base' do + result = described_class.new(keyword: FFaker::Lorem.word).call - expect(result).to be_an_instance_of(HTTParty::Response) - end + expect(result).to be_an_instance_of(HTTParty::Response) + end - it 'queries Google Search', vcr: 'google_search/base' do - path = described_class.new(keyword: FFaker::Lorem.word).call.request.path + it 'queries Google Search', vcr: 'google_search/base' do + path = described_class.new(keyword: FFaker::Lorem.word).call.request.path - expect(path.to_s).to start_with(described_class::BASE_SEARCH_URL) + expect(path.to_s).to start_with(described_class::BASE_SEARCH_URL) + end end - end - context 'when google returns an HTTP error' do - it 'returns false', vcr: 'google_search/too_many_requests' do - result = described_class.new(keyword: FFaker::Lorem.word).call + context 'when google returns an HTTP error' do + it 'returns false', vcr: 'google_search/too_many_requests' do + result = described_class.new(keyword: FFaker::Lorem.word).call - expect(result).to eq(false) - end + expect(result).to eq(false) + end - it 'logs a warning with the escaped keyword', vcr: 'google_search/too_many_requests' do - allow(Rails.logger).to receive(:warn) + it 'logs a warning with the escaped keyword', vcr: 'google_search/too_many_requests' do + allow(Rails.logger).to receive(:warn) - word = FFaker::Lorem.word - described_class.new(keyword: word).call + word = FFaker::Lorem.word + described_class.new(keyword: word).call - expect(Rails.logger).to have_received(:warn).with(/#{CGI.escape(word)}/) + expect(Rails.logger).to have_received(:warn).with(/#{CGI.escape(word)}/) + end end end end diff --git a/spec/services/google/parser_service_spec.rb b/spec/services/google/parser_service_spec.rb index ed9b6894..9ebd135d 100644 --- a/spec/services/google/parser_service_spec.rb +++ b/spec/services/google/parser_service_spec.rb @@ -3,50 +3,52 @@ require 'rails_helper' RSpec.describe Google::ParserService, type: :service do - context 'when parsing a page having 1 top ad' do - it 'counts exactly 1 top ad', vcr: 'google_search/top_ads_1' do - result = Google::ClientService.new(keyword: 'squarespace').call + describe '#call' do + context 'when parsing a page having 1 top ad' do + it 'counts exactly 1 top ad', vcr: 'google_search/top_ads_1' do + result = Google::ClientService.new(keyword: 'squarespace').call - expect(described_class.new(html_response: result).call[:ads_top_count]).to eq(1) + expect(described_class.new(html_response: result).call[:ads_top_count]).to eq(1) + end end - end - context 'when parsing a page having 3 top ads, 3 bottom ads and 14 non ad links' do - it 'counts exactly 3 top ads', vcr: 'google_search/top_ads_6' do - result = Google::ClientService.new(keyword: 'vpn').call + context 'when parsing a page having 3 top ads, 3 bottom ads and 14 non ad links' do + it 'counts exactly 3 top ads', vcr: 'google_search/top_ads_6' do + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:ads_top_count]).to eq(3) - end + expect(described_class.new(html_response: result).call[:ads_top_count]).to eq(3) + end - it 'counts exactly 6 ads in total', vcr: 'google_search/top_ads_6' do - result = Google::ClientService.new(keyword: 'vpn').call + it 'counts exactly 6 ads in total', vcr: 'google_search/top_ads_6' do + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:ads_page_count]).to eq(6) - end + expect(described_class.new(html_response: result).call[:ads_page_count]).to eq(6) + end - it 'finds exactly the 3 top ads urls', vcr: 'google_search/top_ads_6' do - result = Google::ClientService.new(keyword: 'vpn').call + it 'finds exactly the 3 top ads urls', vcr: 'google_search/top_ads_6' do + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:ads_top_url]).to contain_exactly('https://cloud.google.com/free', 'https://www.expressvpn.com/', 'https://www.top10vpn.com/best-vpn-for-vietnam/') - end + expect(described_class.new(html_response: result).call[:ads_top_url]).to contain_exactly('https://cloud.google.com/free', 'https://www.expressvpn.com/', 'https://www.top10vpn.com/best-vpn-for-vietnam/') + end - it 'counts exactly 14 non ad results', vcr: 'google_search/top_ads_6' do - result = Google::ClientService.new(keyword: 'vpn').call + it 'counts exactly 14 non ad results', vcr: 'google_search/top_ads_6' do + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:non_ads_result_count]).to eq(14) - end + expect(described_class.new(html_response: result).call[:non_ads_result_count]).to eq(14) + end - it 'gets 14 results', vcr: 'google_search/top_ads_6' do - result = Google::ClientService.new(keyword: 'vpn').call + it 'gets 14 results', vcr: 'google_search/top_ads_6' do + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:non_ads_url].count).to eq(14) - end + expect(described_class.new(html_response: result).call[:non_ads_url].count).to eq(14) + end - it 'gets exactly 113 links', vcr: 'google_search/top_ads_6' do - # Counted from cassette html raw code - result = Google::ClientService.new(keyword: 'vpn').call + it 'gets exactly 113 links', vcr: 'google_search/top_ads_6' do + # Counted from cassette html raw code + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).call[:total_link_count]).to eq(113) + expect(described_class.new(html_response: result).call[:total_link_count]).to eq(113) + end end end end