- Home
- .NET code examples
- TimeProvider example for mocking time and timer in xUnit
TimeProvider example for mocking time and timer in xUnit
The TimeProvider abstract class is a .NET 8 feature that provides the abstractions to be able to mock time in a unit test.
FakeTimeProvider class, we can set the current UTC time and local time zone. In-addition, we can also create a timer.
Software
As .NET 8 is currently in preview, you'll need the preview version of Visual Studio 2022 (version 17.8 preview) and .NET 8 SDK (RC 2 or above).
Get the application working
These are the steps to get the application working.
- Fill out the code example form. We will send you an email where you can download the code example.
You are now ready to go.
Open the project in Visual Studio
Open up RoundTheCode.TimeProviderMock.sln in Visual Studio 2022 Preview.
TimeTests class.
More information
Watch our video where we go ahead and use the TimeProvider class to set up these unit tests.
Give us your anonymous feedback regarding this page, or any other areas of the website.
Watch .NET tutorials
Subscribe to our YouTube channel
Related code examples
Using Moq in an xUnit project to unit test a .NET project
Download an xUnit and a .NET class library project that uses the Moq mocking framework to mock objects as part of unit tests.