In my Unit test file using Nunit, I am attempting to write test cases so as to test for all of the if/else branches.
Is there a way to inject a specific DateTime.Now
inside this method while calling it in the unit test?
The method takes Opening/Closing times for a restaurant.
public void LunchDinnerBummer(string openingTime, string closingTime)
{
//Based on the current time, alerts the user
//if it is lunch/dinner time or outside of
//business hours
var openTime = DateTime.Parse(openingTime);
var closeTime = DateTime.Parse(closingTime);
//End of lunch time
var lunchTime = DateTime.Parse("3:00 PM");
//For lunch time
if (openTime < DateTime.Now && DateTime.Now < lunchTime)
Console.WriteLine("It is time to go to Ted’s for lunch!");
//For dinner time
else if (DateTime.Now > lunchTime && DateTime.Now < closeTime)
Console.WriteLine("It is time to go to Ted’s for dinner!");
//If outside of business hour before Opening Time for today
else if (DateTime.Now < openTime)
{
TimeSpan span = openTime.Subtract(DateTime.Now);
Console.WriteLine("Bummer, Ted’s is closed");
Console.WriteLine("Ted’s will open in: " + span.Hours + " hour " + " and " + span.Minutes + " minutes ");
}
//If outside of business hours past closing time for today
//Calculate for the hours and minutes left till opening time for next day
else
{
var openTimeNextDay = openTime.AddDays(1);
TimeSpan span = openTimeNextDay.Subtract(DateTime.Now);
Console.WriteLine("Bummer, Ted’s is closed");
Console.WriteLine("Ted’s will open in: " + span.Hours + " hour " + " and " + span.Minutes + " minutes ");
}
}