By default dates and times (DateTime
, DateTimeOffset
, DateOnly
, and TimeOnly
) are sanitized during verification. This is done by finding each date and taking a counter based that that specific date. That counter is then used replace the date values. This allows for repeatable tests when date values are changing.
var dateTime = DateTime.Now;
var dateTimeOffset = DateTimeOffset.Now;
var target = new DateTimeTarget
{
DateTime = dateTime,
Date = new(dateTime.Year, dateTime.Month, dateTime.Day),
DateNullable = new(dateTime.Year, dateTime.Month, dateTime.Day),
DateString = new Date(dateTime.Year, dateTime.Month, dateTime.Day).ToString(),
DateTimeNullable = dateTime,
DateTimeString = dateTime.ToString("F"),
DateTimeOffset = dateTimeOffset,
DateTimeOffsetNullable = dateTimeOffset,
DateTimeOffsetString = dateTimeOffset.ToString("F")
};
await Verify(target);
Results in the following:
{
DateTime: DateTime_1,
DateTimeNullable: DateTime_1,
Date: Date_1,
DateNullable: Date_1,
DateTimeOffset: DateTimeOffset_1,
DateTimeOffsetNullable: DateTimeOffset_1,
DateTimeString: DateTimeOffset_2,
DateTimeOffsetString: DateTimeOffset_2,
DateString: Date_1
}
To disable this behavior use:
var target = new
{
Date = new DateTime(2020, 10, 10, 0, 0, 0, DateTimeKind.Utc)
};
var settings = new VerifySettings();
settings.DontScrubDateTimes();
return Verify(target, settings);
var target = new
{
Date = new DateTime(2020, 10, 10, 0, 0, 0, DateTimeKind.Utc)
};
return Verify(target)
.DontScrubDateTimes();
[ModuleInitializer]
public static void ModuleInitializer() =>
VerifierSettings.DontScrubDateTimes();
If many calls are made to the the current date/time in quick succession, the date counting behavior (DateTime_x
) can result in inconsistent results. To revert to the simpler scrubbing convention ({Scrubbed}
) use DisableDateCounting.
var target = new
{
Date = new DateTime(2020, 10, 10, 0, 0, 0, DateTimeKind.Utc)
};
var settings = new VerifySettings();
settings.DisableDateCounting();
return Verify(target, settings);
var target = new
{
Date = new DateTime(2020, 10, 10, 0, 0, 0, DateTimeKind.Utc)
};
return Verify(target)
.DisableDateCounting();
[ModuleInitializer]
public static void ModuleInitializer() =>
VerifierSettings.DisableDateCounting();
AddExtraDatetimeFormat
allows specifying custom date formats to be scrubbed.
[ModuleInitializer]
public static void UseAddExtraDatetimeFormat() =>
VerifierSettings.AddExtraDatetimeFormat("yyyy-MM-dd");
[Fact]
public Task WithExtraDatetimeFormat() =>
Verify(
new
{
date = "2022-11-08"
});
Strings containing inline dates can also be scrubbed. There a equivalent APIs for DateOnly
, DateTime
, and DateTimeOffset
.
[Fact]
public Task ScrubInlineDateTimesInstance()
{
var settings = new VerifySettings();
settings.ScrubInlineDateTimes("yyyy-MM-dd");
return Verify(
"content 2020-10-20 content",
settings);
}
[Fact]
public Task ScrubInlineDateTimesFluent() =>
Verify("content 2020-10-20 content")
.ScrubInlineDateTimes("yyyy-MM-dd");
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.ScrubInlineDateTimes("yyyy-MM-dd");
}
Specific date or times can be named. When any of those values are found, they will be matched with the corresponding name.
var settings = new VerifySettings();
settings.AddNamedDate(new(2020, 10, 11), "instanceNamedDate");
settings.AddNamedTime(new(1, 2), "instanceTime");
settings.AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime");
settings.AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset");
await Verify(target, settings);
await Verify(target)
.AddNamedDate(new(2020, 10, 11), "instanceNamedDate")
.AddNamedTime(new(1, 2), "instanceTime")
.AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime")
.AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset");
[ModuleInitializer]
public static void NamedDatesAndTimesGlobal()
{
VerifierSettings.AddNamedDateTime(new(2030, 1, 1), "namedDateTime");
VerifierSettings.AddNamedTime(new(1, 1), "namedTime");
VerifierSettings.AddNamedDate(new(2030, 1, 1), "namedDate");
VerifierSettings.AddNamedDateTimeOffset(new(new(2030, 1, 1)), "namedDateTimeOffset");
}