Tuesday, August 9, 2011

of Testing Clocks

I've just emerged from the daily stand up meeting and I have realized that my approach to timekeeping given in my last post was not what was desired. I have made this change thusly.

using System;

namespace MyApp.Core.Utils

{

   public static class DateService

   {

      public static Func<DateTime> Now = () => DateTime.UtcNow;

   }

}

 
This makes the clock testable per this suggestion.

using System;

using MyApp.Core.Utils;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyApp.Core.Tests.Utils

{

   [TestClass]

   public class DateServiceTest

   {

      [TestMethod]

      public void Util_DateService_GivesDate()

      {

         DateTime dateTime = new DateTime(2000, 1, 1);

         DateService.Now = () => new DateTime(2000, 1, 1);

         Assert.AreEqual(DateService.Now(), dateTime);

      }

   }

}

No comments:

Post a Comment