Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not write on negated matcher #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/rspec/snapshot/matchers/match_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions spec/rspec/snapshot/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down