Reputation: 786
In our application we have a method that calls a static class. This class has a method that gets information from a database.
The Load method is called on the Startup.cs of the application, thus, it only runs once. The rationale behind this approach is since the list of holidays only changes once a year, it doesn't make sense to get this information from the database for every single request made to GetHolidayNames()
.
// Method calling the static class
public class HolidayService()
{
public List<string> GetHolidayNames()
{
var result = Loader.HolidayNames;
return result;
}
}
// Static class
public static class Loader
{
public static List<string> HolidayNames;
public static void Load(IUnitOfWork unitOfWork)
{
HolidayNames = unitOfWork.Holidays.GetHolidayNames();
return;
}
}
Is it still possible to unit test GetHolidayNames()
given this scenario?
Upvotes: 1
Views: 1238
Reputation: 559
I think it is still possible to unit test. Given your code snippet I've created a unit test with xUnit and Moq that I believe will allow for the GetHolidayNames()
to be unit tested. I understand that the assertion at the end isn't probably what you're looking for, but I've added it to complete this example.
[Fact]
public void TestHolidayService()
{
// Use Moq to create a mock of your IUnitOfWork
var mockUnitOfWork = new Mock<IUnitOfWork>();
// Setup the getter which is called on IUnitOfWork (may need to set up more state for .GetHolidayNames())
mockUnitOfWork.SetupGet(x => x.Holidays).Returns(new Holiday());
// Call the Load method with mocked IUnitOfWork
Loader.Load(mockUnitOfWork.Object);
var sut = new HolidayService();
var result = sut.GetHolidayNames();
Assert.NotNull(result);
}
Upvotes: 2