- 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.
Related code examples