Home » New in .NET 8.0 [17]: Time abstraction for tests with time specifications

New in .NET 8.0 [17]: Time abstraction for tests with time specifications

by admin
New in .NET 8.0 [17]: Time abstraction for tests with time specifications

In the base class library, Microsoft .NET 8.0 has added the abstract class TimeProvider, a simple way to simulate time information, including the time zone, as part of tests.

Advertisement

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which, with 53 renowned experts, supports numerous medium-sized and large companies through consulting and training as well as with software development. Through his appearances at numerous national and international conferences as well as more than 90 specialist books and more than 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

Many developers have already written an abstraction for the current time instead of using System.DateTime.Now, System.DateTime.UtcNow and System.DateTimeOffset.Now, because real unit tests for program code that depend on the time or date is, needs an abstraction. Otherwise you would have to wait until a certain time or date to start the tests.

Microsoft now provides a built-in abstraction in .NET 8.0. The time specifications in Task.Delay() and Task.Async() can also use this.

The following listing shows several aspects:

TimeProvider.System returns a TimeProvider with the real time according to the system clock.FakeTimeProvider is a class from Microsoft that is supplied in the NuGet package Microsoft.Extensions.TimeProvider.Testing. This class allows a fixed time specification, which can be introduced manually with Advance() by specifying a TimeSpan object or with each readout, for example fakeTime.AutoAdvanceAmount = TimeSpan.FromHours(1). class FCL_TimeProvider { public async Task Run() { void PrintTime(TimeProvider timeProvider) { Console.Write(“Local time: ” + timeProvider.GetLocalNow()); Console.WriteLine(” | UTC: ” + timeProvider.GetUtcNow()); } void Callback(TimeProvider timeProvider) { Console.WriteLine(“Time with ” + timeProvider.GetType()); PrintTime(timeProvider); } CUI.H2(“Regular system time”); PrintTime(TimeProvider.System); // // Microsoft.Extensions.TimeProvider.Testing/ CUI.H2(“Fake time time provider from “+ “Microsoft.Extensions.TimeProvider.Testing”); var fakeTime = new FakeTimeProvider(new(DateTime.Parse(“11/14/2023 6:00:00 PM”))); PrintTime(fakeTime); // November 14th, 2023 6 p.m. Console.WriteLine(“We are now setting the time forward 30 minutes”); fakeTime.Advance(TimeSpan.FromMinutes(30)); // November 14, 2023 6:30 p.m.: Console.WriteLine(“UTC: ” + fakeTime.GetUtcNow()); Console.WriteLine(“We advance the time by 1 hour each time we read ” + “the time”); fakeTime.AutoAdvanceAmount = TimeSpan.FromHours(1); // November 14, 2023 7:30 p.m.: Console.WriteLine(“UTC: ” + fakeTime.GetUtcNow()); // November 14, 2023 8:30 p.m.: Console.WriteLine(“UTC: ” + fakeTime.GetUtcNow()); // November 14, 2023 9:30 p.m.: Console.WriteLine(“UTC: ” + fakeTime.GetUtcNow()); async Task Wait5Seconds(TimeProvider t) { CUI.Cyan(“Waiting begins”); PrintTime(TimeProvider.System); await t.Delay(new TimeSpan(0, 0, 6)); // or //await Task.Delay(new TimeSpan(0, 0, 5), fakeTimeProvider); PrintTime(TimeProvider.System); CUI.Cyan(“Waiting finished!”); } CUI.H2(“6 seconds waiting for TimeProvider.System”); await Wait5Seconds(TimeProvider.System); CUI.H2(“6 seconds waiting for FakeTimeProvider from Microsoft”); var IncreaseTimeAfterOneSecondby10Seconds = async () => { await Task.Delay(new TimeSpan(0, 0, 1)); fakeTime.Advance(TimeSpan.FromSeconds(10)); }; #pragma warning disable CS4014 IncreaseTimeAfterOneSecondBy10Seconds(); #pragma warning restore CS4014 //–> this never ends if you don’t call Advance()!: await Wait5Seconds(fakeTime); } }

In the online conference betterCode() .NET 9.0 on November 19, 2024 by iX and dpunkt.verlag .NET experts from www.IT-Visions.de will present the finished version of .NET 9.0 using practical examples. These include the innovations regarding the .NET 9.0 SDK, C# 13.0, ASP.NET Core 9.0, Blazor 9.0, OR mapping with Entity Framework Core 9.0, Windows Forms 9.0, WPF 9.0, WinUI, cross-platform development with .NET MAUI 9.0 and an outlook on .NET 10.0.

See also  New in .NET 8.0 [5]: Typaliasse in C# 12.0

Ticket sales have already started: discounted blind bird tickets are available before the program is announced.

(rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy