Skip to content

Commit d4c04ff

Browse files
authored
Refactor test cases for AnnotateRoutes.remove_annotations (#748)
Refactor of test cases for AnnotateRoutes.remove_annotations after #736.
1 parent 3be824b commit d4c04ff

File tree

1 file changed

+79
-59
lines changed

1 file changed

+79
-59
lines changed

spec/lib/annotate/annotate_routes_spec.rb

+79-59
Original file line numberDiff line numberDiff line change
@@ -319,96 +319,116 @@
319319
end
320320
end
321321

322-
describe 'When removing' do
323-
before(:each) do
324-
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
325-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
326-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED)
322+
describe '.remove_annotations' do
323+
before :each do
324+
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
325+
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
326+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
327327
end
328328

329-
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
330-
route_file_content = <<~EOS
329+
context 'When trailing annotation exists' do
330+
let :route_file_content do
331+
<<~EOS
331332
332333
333334
334-
ActionController::Routing...
335-
foo
335+
ActionController::Routing...
336+
foo
336337
337338
338-
# == Route Map
339-
#
340-
# another good line
341-
# good line
342-
EOS
339+
# == Route Map
340+
#
341+
# another good line
342+
# good line
343+
EOS
344+
end
343345

344-
expected_result = <<~EOS
346+
let :expected_result do
347+
<<~EOS
345348
346349
347350
348-
ActionController::Routing...
349-
foo
350-
EOS
351+
ActionController::Routing...
352+
foo
353+
EOS
354+
end
355+
356+
it 'removes trailing annotation and trim trailing newlines, but leave leading newlines alone' do
357+
expect(mock_file).to receive(:puts).with(expected_result).once
358+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
351359

352-
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
353-
expect(mock_file).to receive(:puts).with(expected_result)
354-
AnnotateRoutes.remove_annotations
360+
AnnotateRoutes.remove_annotations
361+
end
355362
end
356363

357-
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
358-
route_file_content = <<~EOS
359-
# == Route Map
360-
#
361-
# another good line
362-
# good line
364+
context 'When prepended annotation exists' do
365+
let :route_file_content do
366+
<<~EOS
367+
# == Route Map
368+
#
369+
# another good line
370+
# good line
363371
364372
365373
366374
367-
Rails.application.routes.draw do
368-
root 'root#index'
369-
end
375+
Rails.application.routes.draw do
376+
root 'root#index'
377+
end
370378
371379
372380
373-
EOS
381+
EOS
382+
end
374383

375-
expected_result = <<~EOS
376-
Rails.application.routes.draw do
377-
root 'root#index'
378-
end
384+
let :expected_result do
385+
<<~EOS
386+
Rails.application.routes.draw do
387+
root 'root#index'
388+
end
379389
380390
381391
382-
EOS
392+
EOS
393+
end
383394

384-
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
385-
expect(mock_file).to receive(:puts).with(expected_result)
386-
AnnotateRoutes.remove_annotations
395+
it 'removes prepended annotation and trim leading newlines, but leave trailing newlines alone' do
396+
expect(mock_file).to receive(:puts).with(expected_result).once
397+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
398+
399+
AnnotateRoutes.remove_annotations
400+
end
387401
end
388402

389-
it 'should not remove custom comments above route map' do
390-
route_file_content = <<~EOS
391-
# My comment
392-
# == Route Map
393-
#
394-
# another good line
395-
# good line
396-
Rails.application.routes.draw do
397-
root 'root#index'
398-
end
399-
EOS
403+
context 'When custom comments are above route map' do
404+
let :route_file_content do
405+
<<~EOS
406+
# My comment
407+
# == Route Map
408+
#
409+
# another good line
410+
# good line
411+
Rails.application.routes.draw do
412+
root 'root#index'
413+
end
414+
EOS
415+
end
400416

401-
expected_result = <<~EOS
402-
# My comment
403-
Rails.application.routes.draw do
404-
root 'root#index'
405-
end
406-
EOS
417+
let :expected_result do
418+
<<~EOS
419+
# My comment
420+
Rails.application.routes.draw do
421+
root 'root#index'
422+
end
423+
EOS
424+
end
407425

408-
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content)
409-
expect(mock_file).to receive(:puts).with(expected_result)
426+
it 'does not remove custom comments above route map' do
427+
expect(mock_file).to receive(:puts).with(expected_result).once
428+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_REMOVED).once
410429

411-
AnnotateRoutes.remove_annotations
430+
AnnotateRoutes.remove_annotations
431+
end
412432
end
413433
end
414434
end

0 commit comments

Comments
 (0)