dev-resources.site
for different kinds of informations.
Unit Test And Time Dependency
Published at
9/25/2017
Categories
designpattern
testing
dotnet
tutorial
Author
Matheus Rodrigues
When you start to write unit tests, inevitably you’ll encounter a hard time when a functionality depends on time. Depend on DateTime.Now doesn’t work well for unit testing, your tests will pass on certain time of day and fail in others.
To resolve this problem we need to isolate the time dependency of system, to be able to make our unit test reliable.
Example
So, we have a shop that gives some discount based on the hour of the day, and we have a method that return how much the discount will be:
public class Shop
{
//Constructor
//public Shop(.....
//Other Implementations
//.....
public int GetCurrentDiscount()
{
switch (DateTime.Now.Hour)
{
case 9:
return 30;
case 12:
return 20;
case 18:
return 40;
default:
return 0;
}
}
}
Articles
12 articles in total
Integration Tests In ASP.NET Core Controllers
read article
Unit Tests In ASP.NET Core Controllers
read article
Improving Unit Test Readability In .Net With Fluent Assertions
read article
Mock Multiple Calls To The Same Method With FakeItEasy, Moq and NSubstitute
read article
How To: Use EntityFramework Core In-Memory Database For Unit Testing
read article
Getting Started With MongoDB Using ASP.NET Core Web API
read article
Design Patterns and Practices in .Net: Fluent Builder Test Pattern
read article
How To: Create a Simple Alert System Using Tag Helpers In Asp.Net Core Mvc
read article
Design Patterns and Practices in .Net: Option Functional Type in C#
read article
Design Patterns and Practices in .Net: Null Object and Special Case Object
read article
How To Get appsettings.json Data In Asp.Net Core MVC
read article
Unit Test And Time Dependency
currently reading
Featured ones: