From 88caa847438948f84cc325436dc10af460bf6ed6 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Thu, 8 Dec 2022 16:52:46 +0100 Subject: [PATCH] fail the spec whenever we write a snapshot --- .../snapshot/matchers/match_snapshot_spec.rb | 46 ++++++++++++------- spec/rspec/snapshot/matchers_spec.rb | 6 ++- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/spec/rspec/snapshot/matchers/match_snapshot_spec.rb b/spec/rspec/snapshot/matchers/match_snapshot_spec.rb index af030f9..d497b47 100644 --- a/spec/rspec/snapshot/matchers/match_snapshot_spec.rb +++ b/spec/rspec/snapshot/matchers/match_snapshot_spec.rb @@ -237,8 +237,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end @@ -285,8 +285,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end end @@ -335,8 +335,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end @@ -383,8 +383,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end end @@ -609,8 +609,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end @@ -657,8 +657,8 @@ def dump(object) expect(file).to have_received(:close).twice end - it 'returns true' do - expect(@actual).to be(true) + it 'returns false' do + expect(@actual).to be(false) end end end @@ -700,10 +700,24 @@ def dump(object) subject.instance_variable_set(:@actual, actual) end - it 'returns a failure message including the actual and expected' do - expect(subject.failure_message).to( - eq("\nexpected: #{expected}\n got: #{actual}\n") - ) + context 'when should_write? is true' do + it 'returns a failure message including the actual and expected' do + expect(subject.failure_message).to( + eq("failing because we wrote a snapshot") + ) + end + end + + context 'when should_write? is false' do + before { + allow(subject).to receive(:should_write?).and_return(false) + } + + it 'returns a failure message including the actual and expected' do + expect(subject.failure_message).to( + eq("\nexpected: #{expected}\n got: #{actual}\n") + ) + end end end diff --git a/spec/rspec/snapshot/matchers_spec.rb b/spec/rspec/snapshot/matchers_spec.rb index 48a5dea..67105ea 100644 --- a/spec/rspec/snapshot/matchers_spec.rb +++ b/spec/rspec/snapshot/matchers_spec.rb @@ -272,7 +272,9 @@ def dump(object) file.write(original_snapshot_value) file.close # rubocop:disable RSpec/ExpectInHook - expect(updated_snapshot_value).to match_snapshot(snapshot_name) + expect { + expect(updated_snapshot_value).to match_snapshot(snapshot_name) + }.to raise_error # rubocop:enable RSpec/ExpectInHook file = File.new(snapshot_path) @actual = file.read @@ -295,7 +297,9 @@ def dump(object) before do File.unlink(snapshot_path) if File.exist?(snapshot_path) # rubocop:disable RSpec/ExpectInHook + expect { expect(snapshot_value).to match_snapshot(snapshot_name) + }.to raise_error # rubocop:enable RSpec/ExpectInHook file = File.new(snapshot_path) @actual = file.read