Scrubbers run on the final string prior to doing the verification action.
They can be defined at three levels:
- Method: Will run the verification in the current test method.
- Class: Will run for all verifications in all test methods for a test class.
- Global: Will run for test methods on all tests.
Multiple scrubbers can bee defined at each level.
Scrubber are excited in reveres order. So the most recent added method scrubber through to earliest added global scrubber.
Global scrubbers should be defined only once at appdomain startup. In this example the scrubber is configured using the Global Setup of XunitContext. It could also be configured using a Module Initializer.
Usage:
using System.Threading.Tasks;
using NUnit.Framework;
using Verify;
using VerifyNUnit;
[TestFixture]
public class ScrubbersSample
{
VerifySettings classLevelSettings;
public ScrubbersSample()
{
classLevelSettings = new VerifySettings();
classLevelSettings.AddScrubber(s => s.Replace("Three", "C"));
}
[Test]
public Task Simple()
{
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(s => s.Replace("Two", "B"));
return Verifier.Verify("One Two Three", settings);
}
[Test]
public Task AfterJson()
{
var target = new ToBeScrubbed
{
RowVersion = "0x00000000000007D3"
};
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(s => s.Replace("0x00000000000007D3", "TheRowVersion"));
return Verifier.Verify(target, settings);
}
public static class GlobalSetup
{
[OneTimeSetUp]
public static void Setup()
{
Global.AddScrubber(s => s.Replace("One", "A"));
}
}
}
using System.Threading.Tasks;
using Verify;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
public class ScrubbersSample :
VerifyBase
{
VerifySettings classLevelSettings;
public ScrubbersSample(ITestOutputHelper output) :
base(output)
{
classLevelSettings = new VerifySettings();
classLevelSettings.AddScrubber(s => s.Replace("Three", "C"));
}
[Fact]
public Task Simple()
{
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(s => s.Replace("Two", "B"));
return Verify("One Two Three", settings);
}
[Fact]
public Task AfterJson()
{
var target = new ToBeScrubbed
{
RowVersion = "0x00000000000007D3"
};
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(s => s.Replace("0x00000000000007D3", "TheRowVersion"));
return Verify(target, settings);
}
[GlobalSetUp]
public static class GlobalSetup
{
public static void Setup()
{
Global.AddScrubber(s => s.Replace("One", "A"));
}
}
}
Results:
A B C
A B C
{
RowVersion: 'TheRowVersion'
}
{
RowVersion: 'TheRowVersion'
}