Module: Minitest::Assertions

Defined in:
lib/minitest/snapshots/assertion_extensions.rb

Instance Method Summary collapse

Instance Method Details

#assert_matches_snapshot(value, snapshot_name = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/minitest/snapshots/assertion_extensions.rb', line 6

def assert_matches_snapshot(value, snapshot_name = nil)
  snapshot_file = snapshot_path(self.class.name, snapshot_name || (@snapshot_assertion_counter += 1))
  snapshot = Minitest::Snapshots::Serializer.serialize(value)

  if !Minitest::Snapshots.force_updates && File.exist?(snapshot_file)
    assert_equal(
      File.read(snapshot_file),
      snapshot,
      "The value does not match the snapshot (located at #{snapshot_file})"
    )
  else
    if Minitest::Snapshots.lock_snapshots
      assert(
        false,
        "Attempt to create a snapshot failed because writing is prevented by the --lock-snapshots option"
      )
    end

    FileUtils.mkdir_p(File.dirname(snapshot_file))
    File.write(snapshot_file, snapshot)
  end
end