diff --git a/lib/rspec/snapshot/matchers/match_snapshot.rb b/lib/rspec/snapshot/matchers/match_snapshot.rb index 921a83c..91bc038 100644 --- a/lib/rspec/snapshot/matchers/match_snapshot.rb +++ b/lib/rspec/snapshot/matchers/match_snapshot.rb @@ -43,6 +43,13 @@ def initialize(metadata, snapshot_name, config) FileUtils.mkdir_p(File.dirname(@snapshot_path)) end + def does_not_match?(actual) + @actual = serialize(actual) + @expected = read_snapshot + + @actual != @expected + end + def matches?(actual) @actual = serialize(actual) diff --git a/spec/rspec/snapshot/matchers_spec.rb b/spec/rspec/snapshot/matchers_spec.rb index 48a5dea..ad3e537 100644 --- a/spec/rspec/snapshot/matchers_spec.rb +++ b/spec/rspec/snapshot/matchers_spec.rb @@ -284,6 +284,32 @@ def dump(object) end end + context 'and a snapshot file exists' do + let(:original_snapshot_value) { 'foo' } + let(:updated_snapshot_value) { 'bar' } + let(:snapshot_name) { 'update_existing_snapshot' } + let(:snapshot_path) do + current_directory_path.join('__snapshots__', + "#{snapshot_name}.snap") + end + + before do + file = File.new(snapshot_path, 'w+') + file.write(original_snapshot_value) + file.close + # rubocop:disable RSpec/ExpectInHook + expect(updated_snapshot_value).to_not match_snapshot(snapshot_name) + # rubocop:enable RSpec/ExpectInHook + file = File.new(snapshot_path) + @actual = file.read + file.close + end + + it 'ignores the snapshot and updates it to the current value' do + expect(@actual).to_not eq(updated_snapshot_value) + end + end + context 'and a snapshot file does not exist' do let(:snapshot_value) { 'foo' } let(:snapshot_name) { 'update_non_existing_snapshot' }