class RSpec::Snapshot::Matchers::MatchSnapShot

RSpec matcher for snapshot testing

Attributes

actual[R]
expected[R]

Public Class Methods

new(metadata, snapshot_name, config) click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 13
def initialize(metadata, snapshot_name, config)
  @metadata = metadata
  @snapshot_name = snapshot_name
  @config = config
  @serializer = serializer_class.new
  @snapshot_path = File.join(snapshot_dir, "#{@snapshot_name}.snap")
  create_snapshot_dir
end

Public Instance Methods

===(actual)

is the method called when matching an argument

Alias for: matches?
diffable?() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 93
def diffable?
  true
end
failure_message() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 97
def failure_message
  "\nexpected: #{@expected}\n     got: #{@actual}\n"
end
failure_message_when_negated() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 101
def failure_message_when_negated
  "\nexpected: #{@expected} not to match #{@actual}\n"
end
matches?(actual) click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 46
def matches?(actual)
  @actual = serialize(actual)

  write_snapshot

  @expected = read_snapshot

  @actual == @expected
end
Also aliased as: ===

Private Instance Methods

create_snapshot_dir() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 40
        def create_snapshot_dir
  return if Dir.exist?(File.dirname(@snapshot_path))

  FileUtils.mkdir_p(File.dirname(@snapshot_path))
end
read_snapshot() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 86
        def read_snapshot
  file = File.new(@snapshot_path)
  value = file.read
  file.close
  value
end
serialize(value) click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 59
        def serialize(value)
  if value.is_a?(String)
    value
  else
    @serializer.dump(value)
  end
end
serializer_class() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 22
        def serializer_class
  if @config[:snapshot_serializer]
    @config[:snapshot_serializer]
  elsif RSpec.configuration.snapshot_serializer
    RSpec.configuration.snapshot_serializer
  else
    DefaultSerializer
  end
end
should_write?() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 78
        def should_write?
  update_snapshots? || !File.exist?(@snapshot_path)
end
snapshot_dir() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 32
        def snapshot_dir
  if RSpec.configuration.snapshot_dir.to_s == 'relative'
    File.dirname(@metadata[:file_path]) << '/__snapshots__'
  else
    RSpec.configuration.snapshot_dir
  end
end
update_snapshots?() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 82
        def update_snapshots?
  ENV['UPDATE_SNAPSHOTS']
end
write_snapshot() click to toggle source
# File lib/rspec/snapshot/matchers/match_snapshot.rb, line 67
        def write_snapshot
  return unless should_write?

  RSpec.configuration.reporter.message(
    "Snapshot written: #{@snapshot_path}"
  )
  file = File.new(@snapshot_path, 'w+')
  file.write(@actual)
  file.close
end