Home

Awesome

<img src="/src/icon.png" height="30px"> Verify.Assertions

Discussions Build status NuGet Status

Extends Verify to allow assertion callbacks. This enables using assertion libraries to interrogate during serialization. The primary use case for this is when the data structures being verified are either complex or large.

See Milestones for release notes.

NuGet package

https://nuget.org/packages/Verify.Assertions/

Enable

<!-- snippet: enable -->

<a id='snippet-enable'></a>

[ModuleInitializer]
public static void Init() =>
    VerifyAssertions.Initialize();

<sup><a href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-enable' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Usage

Once enable, any assertion library can be used.

The below examples are simplistic for illustrating the usage. In a real world scenario, if data structures being verified are small, then the assertion can happen before or after the the Verify with no need to assert during serialization.

Xunit

<!-- snippet: XunitUsage -->

<a id='snippet-XunitUsage'></a>

[Fact]
public async Task XunitUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => Assert.Equal("value", _.Property));
}

<sup><a href='/src/Tests/Tests.cs#L3-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-XunitUsage' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

NUnit

<!-- snippet: NUnitUsage -->

<a id='snippet-NUnitUsage'></a>

[Test]
public async Task NUnitUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => Assert.That(_.Property, Is.EqualTo("value")));
}

<sup><a href='/src/NUnitTests/Tests.cs#L4-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-NUnitUsage' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

FluentAssertions

<!-- snippet: FluentAssertionsUsage -->

<a id='snippet-FluentAssertionsUsage'></a>

[Fact]
public async Task FluentAssertionsUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => _.Property.Should().Be("value"));
}

<sup><a href='/src/Tests/FluentAssertionsTests.cs#L5-L17' title='Snippet source file'>snippet source</a> | <a href='#snippet-FluentAssertionsUsage' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Shouldly

<!-- snippet: ShouldlyUsage -->

<a id='snippet-ShouldlyUsage'></a>

[Fact]
public async Task ShouldlyUsage()
{
    var nested = new Nested(Property: "value");
    var target = new Target(nested);
    await Verify(target)
        .Assert<Nested>(
            _ => _.Property.ShouldBe("value"));
}

<sup><a href='/src/Tests/ShouldyAssertionsTests.cs#L5-L17' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldlyUsage' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Shared Assertions

Assertions can be added globally.

<!-- snippet: Shared -->

<a id='snippet-Shared'></a>

[ModuleInitializer]
public static void AddSharedAssert() =>
    VerifyAssertions
        .Assert<SharedNested>(
            _ => Assert.Equal("value", _.Property));

[Fact]
public async Task SharedAssert()
{
    var nested = new SharedNested(Property: "value");
    var target = new SharedTarget(nested);
    await Verify(target);
}

<sup><a href='/src/Tests/Tests.cs#L17-L33' title='Snippet source file'>snippet source</a> | <a href='#snippet-Shared' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Icon

Approval designed by Danang Marhendra from The Noun Project.