DateOverride is a simple solution for mocking date properties with private setters in C#.
Install-Package DateOverride
dotnet add package DateOverride
DateOverride can set any DateTime
or DateTimeOffset
property, with the SetDate()
extension method, trough the following syntax:
object.SetDate(property, date);
Where:
- obejct : the object containing the property to be set
- property : the property name as a string
- date : the date which to set the property
public class Item
{
public DateTime CreatedAt { get; private set; }
}
[Fact]
public void Should_set_DateTime()
{
//Arrange
var item = new Item();
var today = DateTime.Now;
var tomorrow = DateTime.Now.AddDays(1);
//Act
item.SetDate(nameof(item.CreatedAt), tomorrow);
//Assert
item.Date.Should().BeAfter(today);
}
Contributions and feature requests are always welcome.