{"id":480,"date":"2010-02-01T18:07:18","date_gmt":"2010-02-01T16:07:18","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=480"},"modified":"2010-02-01T18:07:18","modified_gmt":"2010-02-01T16:07:18","slug":"testing-datetime-now","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/testing-datetime-now","title":{"rendered":"Testing DateTime.Now"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-486\" title=\"clock\" src=\"\/blog\/wp-content\/uploads\/2010\/01\/clock.jpg\" alt=\"\" width=\"211\" height=\"142\" \/><\/p>\n<p>When you are doing Test Driven Development (TDD) many time during the testing phase you&#8217;ll find yourself with a code that depends on current time.<\/p>\n<p>It&#8217;s not possible write tests for this code as <strong>DateTime.Now changes constantly<\/strong>.<\/p>\n<p>Let&#8217;s take a look at following method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic string CreateName(...)\n{\n   return &quot;name &quot; + DateTime.Now;\n}\n<\/pre>\n<p>The solution to this problem is simple. We&#8217;ll introduce <em>Clock<\/em> class with the same interface as <em>DateTime<\/em>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic string CreateName(...)\n{\n   return &quot;name &quot; + Clock.Now;\n}\n<\/pre>\n<p>We&#8217;d like the test to look something like this:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;Test]\npublic void CreateName_AddsCurrentTimeAtEnd()\n{\n    using (Clock.NowIs(new DateTime(2010, 12, 31, 23, 59, 00)))\n    {\n        string name = new ReportNameService().CreateName(...);\n        Assert.AreEqual(&quot;name 2010-12-31 23:59:00&quot;, name);\n    }\n}\n<\/pre>\n<p><strong>Tests should not leave any side effects<\/strong>. This is why we are using IDisposable pattern. After text execution <em>Clock.Now<\/em> is reverted and again returns current time.<\/p>\n<p>Finally this is how <em>Clock<\/em> class looks like:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Clock : IDisposable\n{\n    private static DateTime? _nowForTest;\n\n    public static DateTime Now\n    {\n        get { return _nowForTest ?? DateTime.Now; }\n    }\n\n    public static IDisposable NowIs(DateTime dateTime)\n    {\n        _nowForTest = dateTime;\n        return new Clock();\n    }\n\n    public void Dispose()\n    {\n        _nowForTest = null;\n    }\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you are doing Test Driven Development (TDD) many time during the testing phase you&#8217;ll find yourself with a code that depends on current time. It&#8217;s not possible write tests for this code as DateTime.Now changes constantly. Let&#8217;s take a look at following method: public string CreateName(&#8230;) { return &quot;name &quot; + DateTime.Now; } The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[15,56],"class_list":["post-480","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c","tag-unit-testing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/480"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=480"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/480\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}