From 6de2d6e7d27a7c163972a0f6987c5014781849e7 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Mon, 3 Feb 2020 15:38:19 +0900 Subject: [PATCH] Make purpose of test cases more clear --- spec/lib/annotate/annotate_routes_spec.rb | 284 +++++++++++----------- 1 file changed, 143 insertions(+), 141 deletions(-) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 1d360286f..26ad4c522 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -45,7 +45,7 @@ end context 'When "config/routes.rb" exists' do - before(:each) do + before :each do expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once @@ -53,180 +53,182 @@ end context 'When the result of `rake routes` is present' do - context 'When the file does not contain magic comment' do - let :rake_routes_result do - <<-EOS + context 'When the result of `rake routes` does not contain Rake version' do + context 'When the file does not contain magic comment' do + 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 - EOS - end + EOS + end - let :route_file_content do - '' - end + let :route_file_content do + '' + end - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~EOS + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 - end + # == 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 + end - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations + AnnotateRoutes.do_annotations + end end - end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~EOS + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 - end + # ## 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 + end - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(format_markdown: true) + AnnotateRoutes.do_annotations(format_markdown: true) + end end - end - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~EOS + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 - end + # 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 + end - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end - end - context 'When the file contains magic comments' do - MAGIC_COMMENTS.each do |magic_comment| - describe "magic comment: #{magic_comment.inspect}" do - let :route_file_content do - <<~EOS - #{magic_comment} - EOS - end + context 'When the file contains magic comments' do + MAGIC_COMMENTS.each do |magic_comment| + describe "magic comment: #{magic_comment.inspect}" do + let :route_file_content do + <<~EOS + #{magic_comment} + EOS + end - let :rake_routes_result do - <<-EOS + 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 - EOS - end - - context 'When the file does not contain annotation yet' do - context 'When no option is passed' do - let :expected_result do - <<~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 - end - - it 'annotates normally' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations - end + EOS end - context 'When the option "format_markdown" is passed' do - let :expected_result do - <<~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 - end - - it 'annotates in Markdown format' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations(format_markdown: true) + context 'When the file does not contain annotation yet' do + context 'When no option is passed' do + let :expected_result do + <<~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 + end + + it 'annotates normally' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations + end end - end - - context 'When the options "wrapper_open" and "wrapper_close" are passed' do - let :expected_result do - <<~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 + context 'When the option "format_markdown" is passed' do + let :expected_result do + <<~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 + end + + it 'annotates in Markdown format' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(format_markdown: true) + end end - it 'annotates and wraps annotation with specified words' do - expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once - expect(mock_file).to receive(:puts).with(expected_result).once - expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once - - AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + context 'When the options "wrapper_open" and "wrapper_close" are passed' do + let :expected_result do + <<~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 + end + + it 'annotates and wraps annotation with specified words' do + expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once + expect(mock_file).to receive(:puts).with(expected_result).once + expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once + + AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END') + end end end end