Table of Contents

Class SnapshotAssertion

Namespace
SnapshotAssertions.TUnit
Assembly
SnapshotAssertions.TUnit.dll

TUnit assertion that verifies an actual string matches a baseline snapshot stored on disk. The default file resolution uses the current TUnit test context's class and method names; chain methods (WithName(string), AtPath(string), WithOptions(SnapshotOptions)) override the defaults.

[AssertionExtension("MatchesSnapshot")]
public sealed class SnapshotAssertion : Assertion<string>, IAssertion
Inheritance
Assertion<string>
SnapshotAssertion
Implements
IAssertion
Inherited Members
Assertion<string>.AssertAsync()
Assertion<string>.GetAwaiter()
Assertion<string>.And
Assertion<string>.Or

Remarks

On mismatch or missing baseline, the actual content is written to a sibling .actual.txt file and the assertion fails with both paths and a line-based diff in the failure message. When the SNAPSHOT_ACCEPT environment variable is set to a truthy value (and the CI environment variable is not set), the actual content is instead written over the expected baseline and the assertion passes; this is the accept-mode used to bulk-update snapshots after intentional changes.

Constructors

SnapshotAssertion(AssertionContext<string>)

Initialises the assertion. Called by the TUnit source generator.

public SnapshotAssertion(AssertionContext<string> context)

Parameters

context AssertionContext<string>

The assertion context supplied by TUnit.

Methods

AtPath(string)

Overrides path resolution entirely with an explicit absolute or relative file path to the expected baseline. The actual file is sibling to the expected file.

public SnapshotAssertion AtPath(string filePath)

Parameters

filePath string

The path to the expected baseline file.

Returns

SnapshotAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

filePath is null.

CheckAsync(EvaluationMetadata<string>)

Implements the specific check logic for this assertion. Called after the context has been evaluated. Override this method if your assertion uses the default AssertAsync() flow. If you override AssertAsync() with custom logic (like AndAssertion/OrAssertion), you don't need to implement this.

protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<string> metadata)

Parameters

metadata EvaluationMetadata<string>

Metadata about the evaluation including value, exception, and timing information

Returns

Task<AssertionResult>

The result of the assertion check

GetExpectation()

Gets a human-readable description of what this assertion expects. Used in error messages.

protected override string GetExpectation()

Returns

string

WithName(string)

Overrides the default TUnit-test-derived snapshot name. Useful when multiple snapshots are produced by a single test method (e.g. before/after states).

public SnapshotAssertion WithName(string snapshotName)

Parameters

snapshotName string

The base name (without extension) under the project's Snapshots/ directory.

Returns

SnapshotAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

snapshotName is null.

WithOptions(SnapshotOptions)

Overrides the comparison options (line-ending handling, BOM, trailing whitespace, trailing newline).

public SnapshotAssertion WithOptions(SnapshotOptions options)

Parameters

options SnapshotOptions

The options to apply.

Returns

SnapshotAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

options is null.

WithScrubber(SnapshotScrubber)

Adds a SnapshotScrubber to the pipeline. Multiple .WithScrubber() calls compose left-to-right: the first scrubber receives the raw actual content; each subsequent scrubber receives the previous scrubber's output. All scrubbers in the chain share a single SnapshotScrubberState so recurring volatile values keep a stable indexed token across the snapshot.

public SnapshotAssertion WithScrubber(SnapshotScrubber scrubber)

Parameters

scrubber SnapshotScrubber

The scrubber to append. Use Default for the curated GUID + ISO 8601 + Unix-millis chain, or one of the individual Scrubbers properties / Pattern(string, string) factories.

Returns

SnapshotAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

scrubber is null.