InlineSnapshot Class

Provides methods for validating objects against inline snapshots in test code.

public static class InlineSnapshot

Example

var data = new { FirstName = "John", LastName = "Doe" };
InlineSnapshot.Validate(data, """
    FirstName: John
    LastName: Doe
    """);

Methods

NameDescription
RegisterSourceRootMapping(string, string)Registers a deterministic source-root mapping so generated paths (for example /_/) can be resolved back to real source files on disk.
Validate(object?, InlineSnapshotSettings?, string?, string?, int)Validates that the serialized representation of the subject matches the expected snapshot using the provided settings. If the snapshot doesn't match, the source file is updated with the actual value based on the configured update strategy.
Validate(object?, string?, string?, int)Validates that the serialized representation of the subject matches the expected snapshot. If the snapshot doesn't match, the source file is updated with the actual value based on the configured update strategy.
WithSerializer(HumanReadableSerializerOptions?)Creates a new InlineSnapshotBuilder with a human-readable serializer configured with the specified options.
WithSerializer(SnapshotSerializer)Creates a new InlineSnapshotBuilder with the specified snapshot serializer.
WithSerializer(Action<HumanReadableSerializerOptions>?)Creates a new InlineSnapshotBuilder with a human-readable serializer configured by the specified action.
WithSettings(InlineSnapshotSettings?)Creates a new InlineSnapshotBuilder with the specified settings.
WithSettings(Action<InlineSnapshotSettings>?)Creates a new InlineSnapshotBuilder with settings configured by the specified action.

RegisterSourceRootMapping(string, string)

InlineSnapshot.cs

Registers a deterministic source-root mapping so generated paths (for example /_/) can be resolved back to real source files on disk.

public static void RegisterSourceRootMapping(string mappedPath, string realPath)

Parameters

mappedPath string
Deterministic mapped prefix emitted by the compiler.
realPath string
Real source-root path on disk.

Validate overloads

Validate(object?, InlineSnapshotSettings?, string?, string?, int)

InlineSnapshot.cs

Validates that the serialized representation of the subject matches the expected snapshot using the provided settings. If the snapshot doesn't match, the source file is updated with the actual value based on the configured update strategy.

public static void Validate(object? subject, InlineSnapshotSettings? settings, string? expected, string? filePath = null, int lineNumber = -1)
Parameters
subject object?
The object to validate.
settings InlineSnapshotSettings?
The settings to use for validation. If null, InlineSnapshotSettings.Default is used.
expected string?
The expected snapshot value. This parameter will be automatically updated when the snapshot changes.
filePath string?
The source file path. Automatically populated by the compiler.
lineNumber int
The line number in the source file. Automatically populated by the compiler.

Validate(object?, string?, string?, int)

InlineSnapshot.cs

Validates that the serialized representation of the subject matches the expected snapshot. If the snapshot doesn't match, the source file is updated with the actual value based on the configured update strategy.

public static void Validate(object? subject, string? expected = null, string? filePath = null, int lineNumber = -1)
Parameters
subject object?
The object to validate.
expected string?
The expected snapshot value. This parameter will be automatically updated when the snapshot changes.
filePath string?
The source file path. Automatically populated by the compiler.
lineNumber int
The line number in the source file. Automatically populated by the compiler.

WithSerializer overloads

WithSerializer(HumanReadableSerializerOptions?)

InlineSnapshot.cs

Creates a new InlineSnapshotBuilder with a human-readable serializer configured with the specified options.

public static InlineSnapshotBuilder WithSerializer(HumanReadableSerializerOptions? options)

WithSerializer(SnapshotSerializer)

InlineSnapshot.cs

Creates a new InlineSnapshotBuilder with the specified snapshot serializer.

public static InlineSnapshotBuilder WithSerializer(SnapshotSerializer serializer)
Parameters
serializer SnapshotSerializer
Returns

InlineSnapshotBuilder

WithSerializer(Action<HumanReadableSerializerOptions>?)

InlineSnapshot.cs

Creates a new InlineSnapshotBuilder with a human-readable serializer configured by the specified action.

public static InlineSnapshotBuilder WithSerializer(Action<HumanReadableSerializerOptions>? configure)

WithSettings overloads

WithSettings(InlineSnapshotSettings?)

InlineSnapshot.cs

Creates a new InlineSnapshotBuilder with the specified settings.

public static InlineSnapshotBuilder WithSettings(InlineSnapshotSettings? settings)

WithSettings(Action<InlineSnapshotSettings>?)

InlineSnapshot.cs

Creates a new InlineSnapshotBuilder with settings configured by the specified action.

public static InlineSnapshotBuilder WithSettings(Action<InlineSnapshotSettings>? configure)