Class: RSpec::Snapshot::Matchers::MatchSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/snapshot/matchers/match_snapshot.rb

Overview

RSpec matcher for snapshot testing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, file_operator) ⇒ MatchSnapshot

convert test values to string for writing to snapshot files. snapshot file contents.

Parameters:

  • serializer (#dump) —

    A class instance which responds to #dump to

  • file_operator (FileOperator) —

    Handles reading and writing the



14
15
16
17
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 14

def initialize(serializer, file_operator)
  @serializer = serializer
  @file_operator = file_operator
end

Instance Attribute Details

#actual ⇒ Object (readonly)

Returns the value of attribute actual.



8
9
10
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 8

def actual
  @actual
end

#expected ⇒ Object (readonly)

Returns the value of attribute expected.



8
9
10
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 8

def expected
  @expected
end

Instance Method Details

#description ⇒ Object



50
51
52
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 50

def description
  "to match a snapshot: '#{@file_operator.snapshot_path}'"
end

#diffable? ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 54

def diffable?
  true
end

#failure_message ⇒ Object



58
59
60
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 58

def failure_message
  "\nexpected: #{@expected}\n     got: #{@actual}\n"
end

#failure_message_when_negated ⇒ Object



62
63
64
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 62

def failure_message_when_negated
  "\nexpected: #{@expected} not to match #{@actual}\n"
end

#matches?(actual) ⇒ Boolean Also known as: ===, match

snapshot contents, false otherwise.

Parameters:

  • actual (*) —

    The received test value to compare to a snapshot.

Returns:

  • (Boolean) —

    True if the serialized actual value matches the



22
23
24
25
26
27
28
29
30
# File 'lib/rspec/snapshot/matchers/match_snapshot.rb', line 22

def matches?(actual)
  @actual = serialize(actual)

  write_snapshot(@actual)

  @expected = read_snapshot

  @actual == @expected
end