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

Fail when writing #34

Open
wants to merge 3 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
15 changes: 12 additions & 3 deletions lib/rspec/snapshot/matchers/match_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def matches?(actual)

@expected = read_snapshot

@actual == @expected
if @wrote
false
else
@actual == @expected
end
end

# === is the method called when matching an argument
Expand All @@ -65,6 +69,7 @@ def matches?(actual)

private def write_snapshot
return unless should_write?
@wrote = true

RSpec.configuration.reporter.message(
"Snapshot written: #{@snapshot_path}"
Expand All @@ -75,7 +80,7 @@ def matches?(actual)
end

private def should_write?
update_snapshots? || !File.exist?(@snapshot_path)
!File.exist?(@snapshot_path) || (update_snapshots? && read_snapshot != @actual)
end

private def update_snapshots?
Expand All @@ -98,7 +103,11 @@ def diffable?
end

def failure_message
"\nexpected: #{@expected}\n got: #{@actual}\n"
if @wrote
"failing because we wrote a snapshot"
else
"\nexpected: #{@expected}\n got: #{@actual}\n"
end
end

def failure_message_when_negated
Expand Down
65 changes: 16 additions & 49 deletions spec/rspec/snapshot/matchers/match_snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,12 @@ def dump(object)
expect(serializer).not_to have_received(:dump)
end

it 'opens the snapshot file for writing' do
expect(File).to have_received(:new).with(snapshot_filepath, 'w+')
end

it 'writes the snapshot file with the value' do
expect(file).to have_received(:write).with(value_to_match)
end

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
expect(File).to have_received(:new).with(snapshot_filepath).twice
end

it 'reads the snapshot file' do
expect(file).to have_received(:read)
expect(file).to have_received(:read).twice
end

it 'closes the snapshot file after reading and writing' do
Expand Down Expand Up @@ -258,27 +243,12 @@ def dump(object)
expect(serializer).to have_received(:dump).with(value_to_match)
end

it 'opens the snapshot file for writing' do
expect(File).to have_received(:new).with(snapshot_filepath, 'w+')
end

it 'writes the snapshot file with the serialized value' do
expect(file).to have_received(:write).with(serialized_value)
end

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
expect(File).to have_received(:new).with(snapshot_filepath).twice
end

it 'reads the snapshot file' do
expect(file).to have_received(:read)
expect(file).to have_received(:read).twice
end

it 'closes the snapshot file after reading and writing' do
Expand Down Expand Up @@ -335,8 +305,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

Expand Down Expand Up @@ -383,8 +353,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
Expand Down Expand Up @@ -590,13 +560,6 @@ def dump(object)
expect(file).to have_received(:write).with(value_to_match)
end

it 'logs the snapshot write with the RSpec reporter' do
expect(RSpec.configuration.reporter).to(
have_received(:message)
.with("Snapshot written: #{snapshot_filepath}")
)
end

it 'opens the snapshot file for reading' do
expect(File).to have_received(:new).with(snapshot_filepath)
end
Expand All @@ -609,8 +572,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

Expand Down Expand Up @@ -657,8 +620,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
Expand Down Expand Up @@ -700,6 +663,10 @@ def dump(object)
subject.instance_variable_set(:@actual, actual)
end

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")
Expand Down
12 changes: 10 additions & 2 deletions spec/rspec/snapshot/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ class TestClass
FileUtils.rm_rf(snapshot_dir)

# rubocop:disable RSpec/ExpectInHook
expect(expected).to match_snapshot(snapshot_name)
expect {
expect(expected).to match_snapshot(snapshot_name)
}.to raise_error
# rubocop:enable RSpec/ExpectInHook
file = File.new(snapshot_path)
@actual = file.read
Expand Down Expand Up @@ -272,7 +274,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
Expand All @@ -295,7 +299,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
Expand Down Expand Up @@ -351,7 +357,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
Expand Down