Skip to content

Commit f5cff71

Browse files
committed
Refactor test cases when the result of rake routes is blank
1 parent 65b201b commit f5cff71

File tree

1 file changed

+125
-120
lines changed

1 file changed

+125
-120
lines changed

spec/lib/annotate/annotate_routes_spec.rb

Lines changed: 125 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -234,159 +234,164 @@
234234
end
235235
end
236236
end
237-
end
238-
end
239237

240-
describe 'When the result of `rake routes` is blank' do
241-
before(:each) do
242-
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).once
243-
expect(File).to receive(:read).with(ROUTE_FILE).and_return(route_file_content).once
238+
context 'When the result of `rake routes` is blank' do
239+
let :rake_routes_result do
240+
''
241+
end
244242

245-
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return(rake_routes_result).once
246-
end
243+
context 'When the file does not contain magic comment' do
244+
context 'When the file does not contain annotation yet' do
245+
let :route_file_content do
246+
''
247+
end
247248

248-
let :rake_routes_result do
249-
''
250-
end
249+
context 'When no option is specified' do
250+
let :expected_result do
251+
<<~EOS
251252
252-
context 'When the file does not contain magic comment' do
253-
context 'When the file does not contain annotation yet' do
254-
let :expected_result do
255-
<<~EOS
253+
# == Route Map
254+
#
255+
EOS
256+
end
256257

257-
# == Route Map
258-
#
259-
EOS
260-
end
258+
it 'inserts annotations' do
259+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
260+
expect(mock_file).to receive(:puts).with(expected_result).once
261+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
261262

262-
let :route_file_content do
263-
''
264-
end
263+
AnnotateRoutes.do_annotations
264+
end
265+
end
265266

266-
context 'When no option is specified' do
267-
it 'inserts annotations' do
268-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
269-
expect(mock_file).to receive(:puts).with(expected_result).once
270-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
267+
context 'When the option "ignore_routes" is specified' do
268+
let :expected_result do
269+
<<~EOS
271270
272-
AnnotateRoutes.do_annotations
273-
end
274-
end
271+
# == Route Map
272+
#
273+
EOS
274+
end
275275

276-
context 'When the option "ignore_routes" is specified' do
277-
it 'inserts annotations' do
278-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
279-
expect(mock_file).to receive(:puts).with(expected_result).once
280-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
276+
it 'inserts annotations' do
277+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
278+
expect(mock_file).to receive(:puts).with(expected_result).once
279+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
281280

282-
AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
283-
end
284-
end
281+
AnnotateRoutes.do_annotations(ignore_routes: 'my_route')
282+
end
283+
end
285284

286-
context 'When the option "position_in_routes" is specified as "top"' do
287-
let :expected_result do
288-
<<~EOS
289-
# == Route Map
290-
#
291-
EOS
292-
end
285+
context 'When the option "position_in_routes" is specified as "top"' do
286+
let :expected_result do
287+
<<~EOS
288+
# == Route Map
289+
#
290+
EOS
291+
end
293292

294-
it 'inserts annotations' do
295-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
296-
expect(mock_file).to receive(:puts).with(expected_result).once
297-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
293+
it 'inserts annotations' do
294+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
295+
expect(mock_file).to receive(:puts).with(expected_result).once
296+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
298297

299-
AnnotateRoutes.do_annotations(position_in_routes: 'top')
298+
AnnotateRoutes.do_annotations(position_in_routes: 'top')
299+
end
300+
end
300301
end
301-
end
302-
end
303302

304-
context 'When the file already contains annotation' do
305-
let :route_file_content do
306-
<<~EOS
303+
context 'When the file already contains annotation' do
304+
context 'When no option is specified' do
305+
let :route_file_content do
306+
<<~EOS
307307
308-
# == Route Map
309-
#
310-
EOS
311-
end
308+
# == Route Map
309+
#
310+
EOS
311+
end
312312

313-
it 'should skip annotations if file does already contain annotation' do
314-
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
315-
expect(mock_file).not_to receive(:puts)
316-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
313+
it 'should skip annotations if file does already contain annotation' do
314+
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
315+
expect(mock_file).not_to receive(:puts)
316+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
317317

318-
AnnotateRoutes.do_annotations
318+
AnnotateRoutes.do_annotations
319+
end
320+
end
321+
end
319322
end
320-
end
321-
end
322323

323-
context 'When the file contains magic comments' do
324-
MAGIC_COMMENTS.each do |magic_comment|
325-
describe "magic comment: #{magic_comment.inspect}" do
326-
let :route_file_content do
327-
<<~EOS
328-
#{magic_comment}
329-
Something
330-
EOS
331-
end
324+
context 'When the file contains magic comments' do
325+
MAGIC_COMMENTS.each do |magic_comment|
326+
describe "magic comment: #{magic_comment.inspect}" do
327+
let :route_file_content do
328+
<<~EOS
329+
#{magic_comment}
330+
Something
331+
EOS
332+
end
332333

333-
context 'When the option "position_in_routes" is specified as "top"' do
334-
let :expected_result do
335-
<<~EOS
336-
#{magic_comment}
334+
context 'When the file does not contain annotation yet' do
335+
context 'When the option "position_in_routes" is specified as "top"' do
336+
let :expected_result do
337+
<<~EOS
338+
#{magic_comment}
337339
338-
# == Route Map
339-
#
340+
# == Route Map
341+
#
340342
341-
Something
342-
EOS
343-
end
343+
Something
344+
EOS
345+
end
344346

345-
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
346-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
347-
expect(mock_file).to receive(:puts).with(expected_result).once
348-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
347+
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
348+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
349+
expect(mock_file).to receive(:puts).with(expected_result).once
350+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
349351

350-
AnnotateRoutes.do_annotations(position_in_routes: 'top')
351-
end
352-
end
352+
AnnotateRoutes.do_annotations(position_in_routes: 'top')
353+
end
354+
end
353355

354-
context 'When the option "position_in_routes" is specified as "bottom"' do
355-
let :expected_result do
356-
<<~EOS
357-
#{magic_comment}
358-
Something
356+
context 'When the option "position_in_routes" is specified as "bottom"' do
357+
let :expected_result do
358+
<<~EOS
359+
#{magic_comment}
360+
Something
359361
360-
# == Route Map
361-
#
362-
EOS
363-
end
362+
# == Route Map
363+
#
364+
EOS
365+
end
364366

365-
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
366-
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
367-
expect(mock_file).to receive(:puts).with(expected_result).once
368-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
367+
it 'leaves magic comment on top and adds an empty line between magic comment and annotation' do
368+
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file).once
369+
expect(mock_file).to receive(:puts).with(expected_result).once
370+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_ANNOTATED).once
369371

370-
AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
371-
end
372-
end
372+
AnnotateRoutes.do_annotations(position_in_routes: 'bottom')
373+
end
374+
end
375+
end
373376

374-
context 'When the file already contains annotation' do
375-
let :route_file_content do
376-
<<~EOS
377-
#{magic_comment}
377+
context 'When the file already contains annotation' do
378+
let :route_file_content do
379+
<<~EOS
380+
#{magic_comment}
378381
379-
# == Route Map
380-
#
381-
EOS
382-
end
382+
# == Route Map
383+
#
384+
EOS
385+
end
383386

384-
it 'skips annotations' do
385-
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
386-
expect(mock_file).not_to receive(:puts)
387-
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
387+
it 'skips annotations' do
388+
expect(File).not_to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
389+
expect(mock_file).not_to receive(:puts)
390+
expect(AnnotateRoutes).to receive(:puts).with(MESSAGE_UNCHANGED).once
388391

389-
AnnotateRoutes.do_annotations
392+
AnnotateRoutes.do_annotations
393+
end
394+
end
390395
end
391396
end
392397
end

0 commit comments

Comments
 (0)