SimpleDateTimeProvider 2022.1.19.17
See the version list below for details.
dotnet add package SimpleDateTimeProvider --version 2022.1.19.17
NuGet\Install-Package SimpleDateTimeProvider -Version 2022.1.19.17
<PackageReference Include="SimpleDateTimeProvider" Version="2022.1.19.17" />
paket add SimpleDateTimeProvider --version 2022.1.19.17
#r "nuget: SimpleDateTimeProvider, 2022.1.19.17"
// Install SimpleDateTimeProvider as a Cake Addin #addin nuget:?package=SimpleDateTimeProvider&version=2022.1.19.17 // Install SimpleDateTimeProvider as a Cake Tool #tool nuget:?package=SimpleDateTimeProvider&version=2022.1.19.17
SimpleDateTimeProvider
A simple abstraction over C#'s DateTime.Now
, DateTime.Today
and DateTime.UtcNow
so you can control these values in your tests. No longer do you have to attempt shennanigans in your tests to handle when you need to use those values in your code.
Features
SystemDateTimeProvider
- Abstraction over the top of System.DateTime.MockDateTimeProvider
- Provider used to mock DateTime values in test projects.
Setup
It's easy to get under way using the providers, simply inject the system provider under the IDateTimeProvider
interface in your functional code. If you are using another library you'll know the syntax but follow the same formula.
_ = services.AddSingleton<IDateTimeProvider, SystemDateTimeProvider>();
Create your class and use that registered SystemDateTimeProvider
that we just created via the IDateTimeProvider
interface. Then use the provider to set the DateTime
values in your class.
public class Service
{
private readonly IDateTimeProvider dateTimeProvider;
public Service(IDateTimeProvider dateTimeProvider)
{
this.dateTimeProvider = dateTimeProvider;
}
public string DateTimeNow()
{
return $"DateTime.Now is {this.dateTimeProvider.Now}";
}
}
Testing
The whole purpose of this was to allow for testable code. So now that you have your class above, you can inject the MockDateTimeProvider
in its place to control the DateTime
values in your tests. It's as easy as the sample below.
[Fact]
public void Today_ShouldReturn_MockedToday()
{
// Arrange
var provider = new MockDateTimeProvider();
var service = new Service(provider);
var today = DateTime.Today;
provider.Today = today;
// Act
var result = service.DateTimeToday();
// Assert
_ = result.ShouldBeOfType<string>();
result.ShouldBe($"DateTime.Today is {today}");
}
Support
I'm sharing some of my work here and if it helps you, I'd love it if you'd consider supporting me at Buy Me A Coffee
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.Text.Json (>= 6.0.1)
-
net5.0
- System.Text.Json (>= 6.0.1)
-
net6.0
- System.Text.Json (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2024.11.1.89 | 345 | 11/1/2024 |
2024.10.1.88 | 1,225 | 10/1/2024 |
2024.9.1.87 | 986 | 9/1/2024 |
2024.7.26.86 | 1,711 | 7/26/2024 |
2024.7.1.84 | 1,659 | 7/1/2024 |
2024.6.1.83 | 1,535 | 6/1/2024 |
2024.5.1.82 | 1,964 | 5/1/2024 |
2024.4.1.81 | 895 | 4/1/2024 |
2024.3.1.80 | 1,185 | 3/1/2024 |
2024.2.1.79 | 1,042 | 2/1/2024 |
2024.1.1.78 | 2,162 | 1/1/2024 |
2023.12.15.77 | 318 | 12/15/2023 |
2023.12.15.76 | 115 | 12/15/2023 |
2023.12.15.75 | 117 | 12/15/2023 |
2023.12.14.71 | 132 | 12/14/2023 |
2022.11.13.46 | 27,512 | 11/13/2022 |
2022.10.30.45 | 602 | 10/30/2022 |
2022.10.23.44 | 421 | 10/23/2022 |
2022.10.16.43 | 498 | 10/16/2022 |
2022.10.9.42 | 412 | 10/9/2022 |
2022.10.2.41 | 475 | 10/2/2022 |
2022.9.18.40 | 981 | 9/18/2022 |
2022.9.4.39 | 432 | 9/4/2022 |
2022.8.28.38 | 433 | 8/28/2022 |
2022.8.21.37 | 807 | 8/21/2022 |
2022.8.14.36 | 429 | 8/14/2022 |
2022.8.7.35 | 474 | 8/7/2022 |
2022.7.24.34 | 1,147 | 7/24/2022 |
2022.7.10.33 | 1,528 | 7/10/2022 |
2022.6.19.32 | 1,429 | 6/19/2022 |
2022.6.5.31 | 492 | 6/5/2022 |
2022.5.15.30 | 794 | 5/15/2022 |
2022.5.1.29 | 1,885 | 5/1/2022 |
2022.4.17.28 | 607 | 4/17/2022 |
2022.4.3.27 | 864 | 4/3/2022 |
2022.3.6.26 | 542 | 3/6/2022 |
2022.2.27.25 | 424 | 2/27/2022 |
2022.2.20.24 | 552 | 2/20/2022 |
2022.2.13.23 | 458 | 2/13/2022 |
2022.2.10.22 | 461 | 2/10/2022 |
2022.2.6.20 | 459 | 2/6/2022 |
2022.2.5.19 | 455 | 2/5/2022 |
2022.1.23.18 | 833 | 1/23/2022 |
2022.1.19.17 | 449 | 1/19/2022 |
2022.1.19.15 | 441 | 1/19/2022 |
2022.1.19.14 | 456 | 1/19/2022 |
2022.1.9.13 | 277 | 1/9/2022 |
2021.11.29.12 | 923 | 11/29/2021 |
2021.11.27.10 | 282 | 11/27/2021 |
2021.11.26.9 | 1,659 | 11/26/2021 |
2021.11.23.8 | 284 | 11/23/2021 |
2021.11.22.4 | 305 | 11/22/2021 |
2021.11.22.3 | 319 | 11/22/2021 |
2021.11.22.2 | 478 | 11/22/2021 |