From a9d6ffda7531fbe80e7de0f1938390b9dd837171 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Fri, 13 Feb 2015 13:35:38 -0500 Subject: [PATCH] Refactoring specs, now with less leakage! --- spec/graphics/graphics_save_doc_spec.rb | 50 ++++++++++++------------- spec/input_helpers_spec.rb | 16 +++----- spec/spec_helper.rb | 9 +---- 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/spec/graphics/graphics_save_doc_spec.rb b/spec/graphics/graphics_save_doc_spec.rb index 8043ea7..9daab1d 100644 --- a/spec/graphics/graphics_save_doc_spec.rb +++ b/spec/graphics/graphics_save_doc_spec.rb @@ -24,41 +24,37 @@ describe Squib::Deck, '#save_pdf' do num_cards = 9 args = { file: 'foo.pdf', dir: '_out', margin: 75, gap: 5, trim: 37 } deck = Squib::Deck.new(cards: num_cards, width: 825, height: 1125) - mock_squib_logger(@old_logger) do - expect(Squib.logger).to receive(:debug).at_least(:once) - expect(Cairo::Context).to receive(:new).and_return(@context).exactly(num_cards + 1).times - expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir - - expect_card_place(75, 75) - expect_card_place(831, 75) - expect_card_place(1587, 75) - expect_card_place(2343, 75) - expect_card_place(75, 1131) - expect_card_place(831, 1131) - expect_card_place(1587, 1131) - expect_card_place(2343, 1131) - expect(@context).to receive(:show_page).once - expect_card_place(75, 75) - - deck.save_pdf(args) - end + expect(Squib.logger).to receive(:debug).at_least(:once) + expect(Cairo::Context).to receive(:new).and_return(@context).exactly(num_cards + 1).times + expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir + + expect_card_place(75, 75) + expect_card_place(831, 75) + expect_card_place(1587, 75) + expect_card_place(2343, 75) + expect_card_place(75, 1131) + expect_card_place(831, 1131) + expect_card_place(1587, 1131) + expect_card_place(2343, 1131) + expect(@context).to receive(:show_page).once + expect_card_place(75, 75) + + deck.save_pdf(args) end it 'only does the three cards on a limited range' do num_cards = 9 args = { range: 2..4, file: 'foo.pdf', dir: '_out', margin: 75, gap: 5, trim: 37 } deck = Squib::Deck.new(cards: num_cards, width: 825, height: 1125) - mock_squib_logger(@old_logger) do - expect(Squib.logger).to receive(:debug).at_least(:once) - expect(Cairo::Context).to receive(:new).and_return(@context).exactly(4).times - expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir + expect(Squib.logger).to receive(:debug).at_least(:once) + expect(Cairo::Context).to receive(:new).and_return(@context).exactly(4).times + expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir - expect_card_place(75, 75) - expect_card_place(831, 75) - expect_card_place(1587, 75) + expect_card_place(75, 75) + expect_card_place(831, 75) + expect_card_place(1587, 75) - deck.save_pdf(args) - end + deck.save_pdf(args) end end diff --git a/spec/input_helpers_spec.rb b/spec/input_helpers_spec.rb index 67e608c..7473c1f 100644 --- a/spec/input_helpers_spec.rb +++ b/spec/input_helpers_spec.rb @@ -25,11 +25,9 @@ describe Squib::InputHelpers do context '#layoutify' do it 'warns on the logger when the layout does not exist' do - mock_squib_logger(@old_logger) do - expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice - expect(Squib.logger).to receive(:debug) - expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]}) - end + expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice + expect(Squib.logger).to receive(:debug) + expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]}) end it 'applies the layout in a normal situation' do @@ -88,11 +86,9 @@ describe Squib::InputHelpers do opts = {dir: 'tocreate'} Dir.chdir(output_dir) do FileUtils.rm_rf('tocreate', secure: true) - mock_squib_logger(@old_logger) do - expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once - expect(@deck.send(:dirify, opts, :dir, true)).to eq(opts) - expect(Dir.exists? 'tocreate').to be true - end + expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once + expect(@deck.send(:dirify, opts, :dir, true)).to eq(opts) + expect(Dir.exists? 'tocreate').to be true end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 80b0f69..dafdcb1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -98,20 +98,13 @@ module Squib def logger=(l) @logger = l end - module_function 'logger=' + module_function :logger= class Deck attr_accessor :progress_bar end end -def mock_squib_logger(old_logger) - old_logger = Squib.logger - Squib.logger = instance_double(Logger) - yield - Squib.logger = old_logger -end - def output_dir File.expand_path('../samples/_output', File.dirname(__FILE__)) end