In some scenarios it makes sense to auto-accept any changes as part of a given test run. For example:
- Keeping a text representation of a Database schema in a
.verified.sql
file (see Verify.SqlServer).
Note that auto accepted changes in .verified.
files remain visible in source control tooling.
This can be done using AutoVerify()
:
var settings = new VerifySettings();
settings.AutoVerify();
Or with a delegate:
var settings = new VerifySettings();
settings.AutoVerify(
verifiedFile =>
Path.GetExtension(verifiedFile) == "png");
[Fact]
public Task AutoVerifyFluent() =>
Verify("Value")
.AutoVerify();
Or with a delegate:
[Fact]
public Task AutoVerifyFluentDelegate() =>
Verify("Value")
.AutoVerify(
verifiedFile =>
Path.GetExtension(verifiedFile) == "png");
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.AutoVerify();
}
Or with a delegate:
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
Path.GetExtension(verifiedFile) == "png");
}
OnVerify
takes two actions that are called before and after each verification.OnFirstVerify
is called when there is no verified file.OnVerifyMismatch
is called when a received file does not match the existing verified file.
OnHandlers are called before AutoVerify logic being applied. So for example in the case of OnVerifyMismatch
, both the received and verified file will exist at the point OnVerifyMismatch
is called. Immediately after received will be used to overwrite verified.
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
VerifierSettings.OnVerify(
before: () => Debug.WriteLine("before"),
after: () => Debug.WriteLine("after"));
VerifierSettings.OnFirstVerify(
(receivedFile, receivedText, autoVerify) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(receivedText);
return Task.CompletedTask;
});
VerifierSettings.OnVerifyMismatch(
(filePair, message, autoVerify) =>
{
Debug.WriteLine(filePair.ReceivedPath);
Debug.WriteLine(filePair.VerifiedPath);
Debug.WriteLine(message);
return Task.CompletedTask;
});
}
}
[Fact]
public Task OnCallbacks()
{
var settings = new VerifySettings();
settings.OnVerify(
before: () => Debug.WriteLine("before"),
after: () => Debug.WriteLine("after"));
settings.OnFirstVerify(
(receivedFile, receivedText, autoVerify) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(receivedText);
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(filePair, message, autoVerify) =>
{
Debug.WriteLine(filePair.ReceivedPath);
Debug.WriteLine(filePair.VerifiedPath);
Debug.WriteLine(message);
return Task.CompletedTask;
});
return Verify("value", settings);
}
[Fact]
public Task OnFluentCallbacks() =>
Verify("value")
.OnVerify(
before: () => Debug.WriteLine("before"),
after: () => Debug.WriteLine("after"))
.OnFirstVerify(
(receivedFile, receivedText, autoVerify) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(receivedText);
return Task.CompletedTask;
})
.OnVerifyMismatch(
(filePair, message, autoVerify) =>
{
Debug.WriteLine(filePair.ReceivedPath);
Debug.WriteLine(filePair.VerifiedPath);
Debug.WriteLine(message);
return Task.CompletedTask;
});
By default, when a verify mismatch occurs for text, the content of the received and verified files is included in the exception that is thrown. This results in that text being included in test runners and build output. To omit the content use VerifierSettings.OmitContentFromException
.
To disable diff launching:
var settings = new VerifySettings();
settings.DisableDiff();