Skip to content

Verify is a snapshot testing tool that simplifies the assertion of complex data models and documents.

License

Notifications You must be signed in to change notification settings

VerifyTests/Verify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify

Build status NuGet Status

Verification tool to enable simple approval of complex models using Json.net.

Contents

NuGet package

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

Usage

Class being tested

Given a class to be tested:

public static class ClassBeingTested
{
    public static Person FindPerson()
    {
        return new Person
        {
            Id = new Guid("ebced679-45d3-4653-8791-3d969c4a986c"),
            Title = Title.Mr,
            GivenNames = "John",
            FamilyName = "Smith",
            Spouse = "Jill",
            Children = new List<string>
            {
                "Sam",
                "Mary"
            },
            Address = new Address
            {
                Street = "4 Puddle Lane",
                Country = "USA"
            }
        };
    }
}

snippet source | anchor

Test

It can be tested as follows:

[TestFixture]
public class SampleTest
{
    [Test]
    public Task Simple()
    {
        var person = ClassBeingTested.FindPerson();
        return Verifier.Verify(person);
    }
}

snippet source | anchor

public class SampleTest :
    VerifyBase
{
    [Fact]
    public Task Simple()
    {
        var person = ClassBeingTested.FindPerson();
        return Verify(person);
    }

    public SampleTest(ITestOutputHelper output) :
        base(output)
    {
    }
}

snippet source | anchor

Initial Verification

When the test is initially run will fail with:

First verification. SampleTest.Simple.verified.txt not found.
Verification command has been copied to the clipboard.

The clipboard will contain the following:

cmd /c move /Y "C:\Code\Sample\SampleTest.Simple.received.txt" "C:\Code\Sample\SampleTest.Simple.verified.txt"

If a Diff Tool is detected it will display the diff:

InitialDiff

To verify the result:

  • Execute the command from the clipboard, or
  • Use the diff tool to accept the changes, or
  • Manually copy the text to the new file

This will result in the SampleTest.Simple.verified.txt being created:

{
  GivenNames: 'John',
  FamilyName: 'Smith',
  Spouse: 'Jill',
  Address: {
    Street: '4 Puddle Lane',
    Country: 'USA'
  },
  Children: [
    'Sam',
    'Mary'
  ],
  Id: Guid_1
}

snippet source | anchor

Subsequent Verification

If the implementation of ClassBeingTested changes:

public static class ClassBeingTested
{
    public static Person FindPerson()
    {
        return new Person
        {
            Id = new Guid("ebced679-45d3-4653-8791-3d969c4a986c"),
            Title = Title.Mr,
            // Middle name added
            GivenNames = "John James",
            FamilyName = "Smith",
            Spouse = "Jill",
            Children = new List<string>
            {
                "Sam",
                "Mary"
            },
            Address = new Address
            {
                // Address changed
                Street = "64 Barnett Street",
                Country = "USA"
            }
        };
    }
}

snippet source | anchor

And the test is re run it will fail with

Verification command has been copied to the clipboard.
Assert.Equal() Failure
                                  ↓ (pos 21)
Expected: ···\n  GivenNames: 'John',\n  FamilyName: 'Smith',\n  Spouse: 'Jill···
Actual:   ···\n  GivenNames: 'John James',\n  FamilyName: 'Smith',\n  Spouse:···
                                  ↑ (pos 21)

The clipboard will again contain the following:

cmd /c move /Y "C:\Code\Sample\SampleTest.Simple.received.txt" "C:\Code\Sample\SampleTest.Simple.verified.txt"

And the Diff Tool is will display the diff:

SecondDiff

The same approach can be used to verify the results and the change to SampleTest.Simple.verified.txt is committed to source control along with the change to ClassBeingTested.

Received and Verified

  • All *.verified.txt files should be committed to source control.
  • All *.received.txt files should be excluded from source control.

Not valid json

Note that the output is technically not valid json. Single quotes are used and names are not quoted. The reason for this is to make the resulting output easier to read and understand.

Release Notes

See closed milestones.

Icon

Helmet designed by Leonidas Ikonomou from The Noun Project.

About

Verify is a snapshot testing tool that simplifies the assertion of complex data models and documents.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Languages