From 5935ee29e5a2de48ea79540d2c45bc177dbe7d90 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sat, 4 Jan 2020 07:04:59 +0900 Subject: [PATCH 1/3] Define mock_file using 'let' --- spec/lib/annotate/annotate_routes_spec.rb | 46 ++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index ed19b781d..0da717c9e 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -7,8 +7,12 @@ ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze - def mock_file(stubs = {}) - @mock_file ||= double(File, stubs) + let :stubs do + {} + end + + let :mock_file do + double(File, stubs) end def magic_comments_list_each @@ -56,7 +60,7 @@ def magic_comments_list_each it 'annotate normal' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" # == Route Map # # Prefix Verb URI Pattern Controller#Action @@ -69,7 +73,7 @@ def magic_comments_list_each it 'annotate markdown' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" # ## Route Map # # Prefix | Verb | URI Pattern | Controller#Action @@ -83,7 +87,7 @@ def magic_comments_list_each it 'wraps annotation if wrapper is specified' do expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" # START # == Route Map # @@ -104,7 +108,7 @@ def magic_comments_list_each .and_return("#{magic_comment}\n#{rake_routes_content}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" #{magic_comment} # == Route Map @@ -124,7 +128,7 @@ def magic_comments_list_each .and_return("#{magic_comment}\n#{rake_routes_content}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" #{magic_comment} # ## Route Map @@ -144,7 +148,7 @@ def magic_comments_list_each expect(AnnotateRoutes).to receive(:`).with('rake routes') .and_return("#{magic_comment}\n#{rake_routes_content}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with(" + expect(mock_file).to receive(:puts).with(" #{magic_comment} # START @@ -173,7 +177,7 @@ def magic_comments_list_each it 'should insert annotations if file does not contain annotations' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n") + expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) AnnotateRoutes.do_annotations @@ -182,7 +186,7 @@ def magic_comments_list_each it 'should insert annotations if file does not contain annotations and ignore routes' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n") + expect(mock_file).to receive(:puts).with("\n# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) AnnotateRoutes.do_annotations(ignore_routes: 'my_route') @@ -191,7 +195,7 @@ def magic_comments_list_each it 'should insert annotations if file does not contain annotations and position top' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(@mock_file).to receive(:puts).with("# == Route Map\n#\n") + expect(mock_file).to receive(:puts).with("# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) AnnotateRoutes.do_annotations(position_in_routes: 'top') @@ -211,7 +215,7 @@ def magic_comments_list_each magic_comments_list_each do |magic_comment| expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") - expect(@mock_file).to receive(:puts).with("#{magic_comment}\n\n# == Route Map\n#\n\nSomething\n") + expect(mock_file).to receive(:puts).with("#{magic_comment}\n\n# == Route Map\n#\n\nSomething\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) AnnotateRoutes.do_annotations(position_in_routes: 'top') end @@ -223,7 +227,7 @@ def magic_comments_list_each magic_comments_list_each do |magic_comment| expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") - expect(@mock_file).to receive(:puts).with("#{magic_comment}\nSomething\n\n# == Route Map\n#\n") + expect(mock_file).to receive(:puts).with("#{magic_comment}\nSomething\n\n# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) AnnotateRoutes.do_annotations(position_in_routes: 'bottom') end @@ -251,13 +255,13 @@ def magic_comments_list_each it 'should annotate and add a newline!' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") - expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) + expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) AnnotateRoutes.do_annotations end it 'should not add a newline if there are empty lines' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") - expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) + expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) AnnotateRoutes.do_annotations end end @@ -272,19 +276,19 @@ def magic_comments_list_each it 'should annotate and add a newline!' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") - expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) + expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) AnnotateRoutes.do_annotations end it 'should not add a newline if there are empty lines' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") - expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) + expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) AnnotateRoutes.do_annotations end it 'should add a timestamp when :timestamp is passed' do expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") - expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/) + expect(mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/) AnnotateRoutes.do_annotations timestamp: true end end @@ -311,7 +315,7 @@ def magic_comments_list_each # good line EOS ) - expect(@mock_file).to receive(:puts).with(<<-EOS + expect(mock_file).to receive(:puts).with(<<-EOS @@ -340,7 +344,7 @@ def magic_comments_list_each EOS ) - expect(@mock_file).to receive(:puts).with(<<-EOS + expect(mock_file).to receive(:puts).with(<<-EOS Rails.application.routes.draw do root 'root#index' end @@ -365,7 +369,7 @@ def magic_comments_list_each EOS ) - expect(@mock_file).to receive(:puts).with(<<-EOS + expect(mock_file).to receive(:puts).with(<<-EOS # My comment Rails.application.routes.draw do root 'root#index' From 8e33c0319beb5d95a3af7bed847302d4ecc68a36 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sat, 4 Jan 2020 13:47:56 +0900 Subject: [PATCH 2/3] Add constant MAGIC_COMMENTS --- spec/lib/annotate/annotate_routes_spec.rb | 42 +++++++++++------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 0da717c9e..4ad9095f8 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -7,6 +7,20 @@ ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}.".freeze FILE_UNCHANGED = "#{ROUTE_FILE} unchanged.".freeze + MAGIC_COMMENTS = [ + '# encoding: UTF-8', + '# coding: UTF-8', + '# -*- coding: UTF-8 -*-', + '#encoding: utf-8', + '# encoding: utf-8', + '# -*- encoding : utf-8 -*-', + "# encoding: utf-8\n# frozen_string_literal: true", + "# frozen_string_literal: true\n# encoding: utf-8", + '# frozen_string_literal: true', + '#frozen_string_literal: false', + '# -*- frozen_string_literal : true -*-' + ].freeze + let :stubs do {} end @@ -15,22 +29,6 @@ double(File, stubs) end - def magic_comments_list_each - [ - '# encoding: UTF-8', - '# coding: UTF-8', - '# -*- coding: UTF-8 -*-', - '#encoding: utf-8', - '# encoding: utf-8', - '# -*- encoding : utf-8 -*-', - "# encoding: utf-8\n# frozen_string_literal: true", - "# frozen_string_literal: true\n# encoding: utf-8", - '# frozen_string_literal: true', - '#frozen_string_literal: false', - '# -*- frozen_string_literal : true -*-' - ].each { |magic_comment| yield magic_comment } - end - it 'should check if routes.rb exists' do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false) expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb") @@ -103,7 +101,7 @@ def magic_comments_list_each context 'file with magic comments' do it 'should not remove magic comments' do - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(AnnotateRoutes).to receive(:`).with('rake routes') .and_return("#{magic_comment}\n#{rake_routes_content}") @@ -123,7 +121,7 @@ def magic_comments_list_each end it 'annotate markdown' do - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(AnnotateRoutes).to receive(:`).with('rake routes') .and_return("#{magic_comment}\n#{rake_routes_content}") @@ -144,7 +142,7 @@ def magic_comments_list_each end it 'wraps annotation if wrapper is specified' do - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(AnnotateRoutes).to receive(:`).with('rake routes') .and_return("#{magic_comment}\n#{rake_routes_content}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) @@ -213,7 +211,7 @@ def magic_comments_list_each expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") expect(mock_file).to receive(:puts).with("#{magic_comment}\n\n# == Route Map\n#\n\nSomething\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) @@ -225,7 +223,7 @@ def magic_comments_list_each expect(File).to receive(:open).with(ROUTE_FILE, 'wb') .and_yield(mock_file).at_least(:once) - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(File).to receive(:read).with(ROUTE_FILE).and_return("#{magic_comment}\nSomething") expect(mock_file).to receive(:puts).with("#{magic_comment}\nSomething\n\n# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) @@ -234,7 +232,7 @@ def magic_comments_list_each end it 'skips annotations if file does already contain annotation' do - magic_comments_list_each do |magic_comment| + MAGIC_COMMENTS.each do |magic_comment| expect(File).to receive(:read).with(ROUTE_FILE) .and_return("#{magic_comment}\n\n# == Route Map\n#\n") expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) From b77e99ae20ecfd3a8e6574207f323a78951a6017 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 5 Jan 2020 22:12:43 +0900 Subject: [PATCH 3/3] Refactor long string in RSpec file --- spec/lib/annotate/annotate_routes_spec.rb | 237 ++++++++++++---------- 1 file changed, 134 insertions(+), 103 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 4ad9095f8..30b33e708 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -36,11 +36,13 @@ end describe 'Annotate#example' do - let(:rake_routes_content) do - " Prefix Verb URI Pattern Controller#Action + let :rake_routes_result do + <<-EOS + Prefix Verb URI Pattern Controller#Action myaction1 GET /url1(.:format) mycontroller1#action myaction2 POST /url2(.:format) mycontroller2#action - myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n" + myaction3 DELETE|GET /url3(.:format) mycontroller3#action + EOS end before(:each) do @@ -53,47 +55,59 @@ context 'without magic comments' do before(:each) do - expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_content) + expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result) end it 'annotate normal' do + expected_result = <<~EOS + + # == Route Map + # + # Prefix Verb URI Pattern Controller#Action + # myaction1 GET /url1(.:format) mycontroller1#action + # myaction2 POST /url2(.:format) mycontroller2#action + # myaction3 DELETE|GET /url3(.:format) mycontroller3#action + EOS + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -# == Route Map -# -# Prefix Verb URI Pattern Controller#Action -# myaction1 GET /url1(.:format) mycontroller1#action -# myaction2 POST /url2(.:format) mycontroller2#action -# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations end it 'annotate markdown' do + expected_result = <<~EOS + + # ## Route Map + # + # Prefix | Verb | URI Pattern | Controller#Action + # --------- | ---------- | --------------- | -------------------- + # myaction1 | GET | /url1(.:format) | mycontroller1#action + # myaction2 | POST | /url2(.:format) | mycontroller2#action + # myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action + EOS + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -# ## Route Map -# -# Prefix | Verb | URI Pattern | Controller#Action -# --------- | ---------- | --------------- | -------------------- -# myaction1 | GET | /url1(.:format) | mycontroller1#action -# myaction2 | POST | /url2(.:format) | mycontroller2#action -# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations(format_markdown: true) end it 'wraps annotation if wrapper is specified' do + expected_result = <<~EOS + + # START + # == Route Map + # + # Prefix Verb URI Pattern Controller#Action + # myaction1 GET /url1(.:format) mycontroller1#action + # myaction2 POST /url2(.:format) mycontroller2#action + # myaction3 DELETE|GET /url3(.:format) mycontroller3#action + # END + EOS + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -# START -# == Route Map -# -# Prefix Verb URI Pattern Controller#Action -# myaction1 GET /url1(.:format) mycontroller1#action -# myaction2 POST /url2(.:format) mycontroller2#action -# myaction3 DELETE|GET /url3(.:format) mycontroller3#action -# END\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end @@ -102,19 +116,23 @@ context 'file with magic comments' do it 'should not remove magic comments' do MAGIC_COMMENTS.each do |magic_comment| + expected_result = <<~EOS + + #{magic_comment} + + # == Route Map + # + # Prefix Verb URI Pattern Controller#Action + # myaction1 GET /url1(.:format) mycontroller1#action + # myaction2 POST /url2(.:format) mycontroller2#action + # myaction3 DELETE|GET /url3(.:format) mycontroller3#action + EOS + expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_content}") + .and_return("#{magic_comment}\n#{rake_routes_result}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -#{magic_comment} - -# == Route Map -# -# Prefix Verb URI Pattern Controller#Action -# myaction1 GET /url1(.:format) mycontroller1#action -# myaction2 POST /url2(.:format) mycontroller2#action -# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations end @@ -122,20 +140,24 @@ it 'annotate markdown' do MAGIC_COMMENTS.each do |magic_comment| + expected_result = <<~EOS + + #{magic_comment} + + # ## Route Map + # + # Prefix | Verb | URI Pattern | Controller#Action + # --------- | ---------- | --------------- | -------------------- + # myaction1 | GET | /url1(.:format) | mycontroller1#action + # myaction2 | POST | /url2(.:format) | mycontroller2#action + # myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action + EOS + expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_content}") + .and_return("#{magic_comment}\n#{rake_routes_result}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -#{magic_comment} - -# ## Route Map -# -# Prefix | Verb | URI Pattern | Controller#Action -# --------- | ---------- | --------------- | -------------------- -# myaction1 | GET | /url1(.:format) | mycontroller1#action -# myaction2 | POST | /url2(.:format) | mycontroller2#action -# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations(format_markdown: true) end @@ -143,20 +165,24 @@ it 'wraps annotation if wrapper is specified' do MAGIC_COMMENTS.each do |magic_comment| + expected_result = <<~EOS + + #{magic_comment} + + # START + # == Route Map + # + # Prefix Verb URI Pattern Controller#Action + # myaction1 GET /url1(.:format) mycontroller1#action + # myaction2 POST /url2(.:format) mycontroller2#action + # myaction3 DELETE|GET /url3(.:format) mycontroller3#action + # END + EOS + expect(AnnotateRoutes).to receive(:`).with('rake routes') - .and_return("#{magic_comment}\n#{rake_routes_content}") + .and_return("#{magic_comment}\n#{rake_routes_result}") expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file) - expect(mock_file).to receive(:puts).with(" -#{magic_comment} - -# START -# == Route Map -# -# Prefix Verb URI Pattern Controller#Action -# myaction1 GET /url1(.:format) mycontroller1#action -# myaction2 POST /url2(.:format) mycontroller2#action -# myaction3 DELETE|GET /url3(.:format) mycontroller3#action -# END\n") + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') end @@ -299,81 +325,86 @@ end it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS + route_file_content = <<~EOS - ActionController::Routing... - foo + ActionController::Routing... + foo - # == Route Map - # - # another good line - # good line + # == Route Map + # + # another good line + # good line EOS - ) - expect(mock_file).to receive(:puts).with(<<-EOS + expected_result = <<~EOS - ActionController::Routing... - foo + + ActionController::Routing... + foo EOS - ) + + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.remove_annotations end it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS - # == Route Map - # - # another good line - # good line + route_file_content = <<~EOS + # == Route Map + # + # another good line + # good line - Rails.application.routes.draw do - root 'root#index' - end + Rails.application.routes.draw do + root 'root#index' + end EOS - ) - expect(mock_file).to receive(:puts).with(<<-EOS - Rails.application.routes.draw do - root 'root#index' - end + + expected_result = <<~EOS + Rails.application.routes.draw do + root 'root#index' + end EOS - ) + + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.remove_annotations end it 'should not remove custom comments above route map' do - expect(File).to receive(:read).with(ROUTE_FILE).and_return(<<-EOS - # My comment - # == Route Map - # - # another good line - # good line - Rails.application.routes.draw do - root 'root#index' - end + route_file_content = <<~EOS + # My comment + # == Route Map + # + # another good line + # good line + Rails.application.routes.draw do + root 'root#index' + end EOS - ) - expect(mock_file).to receive(:puts).with(<<-EOS - # My comment - Rails.application.routes.draw do - root 'root#index' - end + expected_result = <<~EOS + # My comment + Rails.application.routes.draw do + root 'root#index' + end EOS - ) + + expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content) + expect(mock_file).to receive(:puts).with(expected_result) AnnotateRoutes.remove_annotations end