Skip to content

Extends Verify to allow an assertion callbacks

License

Notifications You must be signed in to change notification settings

VerifyTests/Verify.Assertions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

snippet source | anchor

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.

[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));
}

snippet source | anchor

[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")));
}

snippet source | anchor

[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"));
}

snippet source | anchor

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

snippet source | anchor

Shared Assertions

Assertions can be added globally.

[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);
}

snippet source | anchor

Icon

Approval designed by Danang Marhendra from The Noun Project.

About

Extends Verify to allow an assertion callbacks

Resources

License

Stars

Watchers

Forks

Sponsor this project