eladaus.TestableTime 1.0.0

dotnet add package eladaus.TestableTime --version 1.0.0                
NuGet\Install-Package eladaus.TestableTime -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="eladaus.TestableTime" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add eladaus.TestableTime --version 1.0.0                
#r "nuget: eladaus.TestableTime, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install eladaus.TestableTime as a Cake Addin
#addin nuget:?package=eladaus.TestableTime&version=1.0.0

// Install eladaus.TestableTime as a Cake Tool
#tool nuget:?package=eladaus.TestableTime&version=1.0.0                

TestableTime

Async-capable Ambient Context Pattern for unit/integration testable DateTimes in C# .NET

For any and all future updates, the code releases will utilizing SemVer semantic versioning style.

Conveniently, eladaus.TestableTime is available on NuGet as eladaus.TestableTime. Install it from NuGet Package Manager Console with:

Install-Package eladaus.TestableTime

Known Issues

  • None currently

Description

The TestableTime library is a modernized, async-capable implementation of a DateTime provider context. It allows developers to retrieve DateTime.UtcNow values in code under normal execution operations but also to mock / programmatically assign the point-in-time for unit testing operations.

It is modelled on the Ambient Context Model (see dvoituron.com in links below) however that code fails under modern asynchronous scenarios due to thread changes during execution.

Using the new AsyncLocal concept, we can now pass a predefined DateTime instance through chained/nested async logic during unit and integration testing.


How to Use:


	var fakeDate = new DateTime(1981, 08, 20, 0, 0, 0, DateTimeKind.Utc);
	using var innerClock = new DateTimeUtcProviderContext(
		fakeDate
	);

	var resultFromLocalContext = DateTimeUtcProvider.UtcNow;
	var resultFromNestedCalls = await new MyAsyncClass().DoSomeAsyncThingReturnTheDate();

	Assert.Equal(resultFromLocalContext, resultFromNestedCalls);
	Assert.Equal(resultFromLocalContext, fakeDate);


Further reading / inspirational resources

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
1.0.0 140 3/23/2024

Initial release