SimpleDateTimeProvider 2024.7.26.86
See the version list below for details.
dotnet add package SimpleDateTimeProvider --version 2024.7.26.86
NuGet\Install-Package SimpleDateTimeProvider -Version 2024.7.26.86
<PackageReference Include="SimpleDateTimeProvider" Version="2024.7.26.86" />
paket add SimpleDateTimeProvider --version 2024.7.26.86
#r "nuget: SimpleDateTimeProvider, 2024.7.26.86"
// Install SimpleDateTimeProvider as a Cake Addin #addin nuget:?package=SimpleDateTimeProvider&version=2024.7.26.86 // Install SimpleDateTimeProvider as a Cake Tool #tool nuget:?package=SimpleDateTimeProvider&version=2024.7.26.86
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 was computed. 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 is compatible. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- System.Text.Json (>= 8.0.4)
-
net6.0
- System.Text.Json (>= 8.0.4)
-
net8.0
- System.Text.Json (>= 8.0.4)
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 | 893 | 11/1/2024 |
2024.10.1.88 | 1,295 | 10/1/2024 |
2024.9.1.87 | 1,027 | 9/1/2024 |
2024.7.26.86 | 1,834 | 7/26/2024 |
2024.7.1.84 | 1,673 | 7/1/2024 |
2024.6.1.83 | 1,569 | 6/1/2024 |
2024.5.1.82 | 1,973 | 5/1/2024 |
2024.4.1.81 | 897 | 4/1/2024 |
2024.3.1.80 | 1,210 | 3/1/2024 |
2024.2.1.79 | 1,045 | 2/1/2024 |
2024.1.1.78 | 2,229 | 1/1/2024 |
2023.12.15.77 | 320 | 12/15/2023 |
2023.12.15.76 | 117 | 12/15/2023 |
2023.12.15.75 | 123 | 12/15/2023 |
2023.12.14.71 | 134 | 12/14/2023 |
2022.11.13.46 | 28,180 | 11/13/2022 |
2022.10.30.45 | 604 | 10/30/2022 |
2022.10.23.44 | 423 | 10/23/2022 |
2022.10.16.43 | 500 | 10/16/2022 |
2022.10.9.42 | 414 | 10/9/2022 |
2022.10.2.41 | 479 | 10/2/2022 |
2022.9.18.40 | 987 | 9/18/2022 |
2022.9.4.39 | 434 | 9/4/2022 |
2022.8.28.38 | 435 | 8/28/2022 |
2022.8.21.37 | 812 | 8/21/2022 |
2022.8.14.36 | 431 | 8/14/2022 |
2022.8.7.35 | 476 | 8/7/2022 |
2022.7.24.34 | 1,152 | 7/24/2022 |
2022.7.10.33 | 1,530 | 7/10/2022 |
2022.6.19.32 | 1,438 | 6/19/2022 |
2022.6.5.31 | 493 | 6/5/2022 |
2022.5.15.30 | 798 | 5/15/2022 |
2022.5.1.29 | 1,891 | 5/1/2022 |
2022.4.17.28 | 611 | 4/17/2022 |
2022.4.3.27 | 866 | 4/3/2022 |
2022.3.6.26 | 544 | 3/6/2022 |
2022.2.27.25 | 426 | 2/27/2022 |
2022.2.20.24 | 554 | 2/20/2022 |
2022.2.13.23 | 460 | 2/13/2022 |
2022.2.10.22 | 462 | 2/10/2022 |
2022.2.6.20 | 461 | 2/6/2022 |
2022.2.5.19 | 456 | 2/5/2022 |
2022.1.23.18 | 835 | 1/23/2022 |
2022.1.19.17 | 451 | 1/19/2022 |
2022.1.19.15 | 443 | 1/19/2022 |
2022.1.19.14 | 458 | 1/19/2022 |
2022.1.9.13 | 279 | 1/9/2022 |
2021.11.29.12 | 924 | 11/29/2021 |
2021.11.27.10 | 284 | 11/27/2021 |
2021.11.26.9 | 1,661 | 11/26/2021 |
2021.11.23.8 | 288 | 11/23/2021 |
2021.11.22.4 | 307 | 11/22/2021 |
2021.11.22.3 | 321 | 11/22/2021 |
2021.11.22.2 | 493 | 11/22/2021 |