﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Anand commented on Dealing with time in tests</title><description>Ayende,
  
  
I might be too sleepy while reading this but  the approach with the "Using" block (that Tuna mentions) ensures that unit tests clear up the DateTime they set for testing in the Dispose() back to DateTime.Now.
  
  
In your case you would have to use a teardown or set it back explicitly.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment14</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment14</guid><pubDate>Thu, 31 Jul 2008 19:22:36 GMT</pubDate></item><item><title>Erich Eichinger commented on Dealing with time in tests</title><description>Greg,
  
you can easily add a "readOnly" flag to SystemTime and allow for modifying Now only in case "readOnly" is false. For testing purposes you can change "readOnly" in your fixture setup. Thus working around this guard would be quite obvious in the code and requires extra effort by a developer.
  
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment13</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment13</guid><pubDate>Tue, 15 Jul 2008 05:45:34 GMT</pubDate></item><item><title>Ayende Rahien commented on Dealing with time in tests</title><description>Greg,
  
I don't. It doesn't make sense to do it for production.
  
I am not trying to verify that people can't do stupid stuff
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment12</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment12</guid><pubDate>Fri, 11 Jul 2008 08:39:11 GMT</pubDate></item><item><title>Greg Beech commented on Dealing with time in tests</title><description>So how do you test that some production code doesn't accidentally do something like:
  
  
SystemTime.Now = () =&gt; someFixedDate;
  
  
Having a read/write property for the time is dangerous if you depend on it to make decisions. And it's virtually impossible to test that you're not doing something like this anywhere it a system.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment11</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment11</guid><pubDate>Fri, 11 Jul 2008 07:29:15 GMT</pubDate></item><item><title>Frank Quednau commented on Dealing with time in tests</title><description>Pff, And there was me calling kernel32.SetLocalTime for such tests. ;)
  
Thanks for the approach!
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment10</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment10</guid><pubDate>Tue, 08 Jul 2008 12:50:20 GMT</pubDate></item><item><title>Ayende Rahien commented on Dealing with time in tests</title><description>Tuna,
  
My way is three lines of code, that is it.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment9</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment9</guid><pubDate>Mon, 07 Jul 2008 19:47:58 GMT</pubDate></item><item><title>Ayende Rahien commented on Dealing with time in tests</title><description>This is not something that you'll likely change for production, it is needed for tests only.
  
It is also something that I _want_ to be cross threaded, I have mutli threaded tests
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment8</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment8</guid><pubDate>Mon, 07 Jul 2008 19:38:30 GMT</pubDate></item><item><title>Tuna Toksoz commented on Dealing with time in tests</title><description>Why wouldn't you go for abstraction? The simplicity?
  
I think that this would be more readable and maybe elegant?
  
  
using(IDisposable time=Time.Frozen("11.11.2011"))
  
{
  
  
}
  
  
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment7</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment7</guid><pubDate>Mon, 07 Jul 2008 16:25:54 GMT</pubDate></item><item><title>Peter Ritchie commented on Dealing with time in tests</title><description>The problem with not doing a full injectionable implementation (i.e. some IDateTime interface, etc.) is that you're now coupled to a different class.  While this new class is more flexible, it's not thread safe--one thread could change the Now delegate and affect all other threads using SystemTime.Now.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment6</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment6</guid><pubDate>Mon, 07 Jul 2008 12:57:12 GMT</pubDate></item><item><title>Ayende Rahien commented on Dealing with time in tests</title><description>Because it is a field that contains a delegate.
  
That allows me to directly switch values without needs to stub it.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment5</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment5</guid><pubDate>Mon, 07 Jul 2008 10:32:06 GMT</pubDate></item><item><title>Joshua McKinney commented on Dealing with time in tests</title><description>I've seen IClock.GetCurrentTime in the codecampserver source. Easy to stub / mock with a class that allows you to specify a time.
  
  
I curious as to why you needed to pull out the Func-y lambda magic just to set a static property here?
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment4</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment4</guid><pubDate>Mon, 07 Jul 2008 10:16:49 GMT</pubDate></item><item><title>Ayende Rahien commented on Dealing with time in tests</title><description>I don't try to enforce that.
  
The tests will make sure that this happen, because if there is time used there, it will be tested, and for that you need SystemTime.
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment3</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment3</guid><pubDate>Mon, 07 Jul 2008 06:15:42 GMT</pubDate></item><item><title>James commented on Dealing with time in tests</title><description>This solution seems to be the most effective way to get reliable tests if there are timing issues involved - We found this out the hard way, a lot of what our products do are timing sensitive, and require actions like backing off for X amount of time, scheduling Y to occur after Z amount of time has elapsed.
  
  
Steve - You could probably use aspects in your debug builds to intercept calls to DateTime.Now, and throw exceptions if that is the case.
  
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment2</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment2</guid><pubDate>Mon, 07 Jul 2008 06:07:47 GMT</pubDate></item><item><title>Steve commented on Dealing with time in tests</title><description>I guess this is similar to introducing a mockable IClock dependency which I have done in the past to time sensitive services.
  
  
Just curious, how do you enforce that you use the SystemTime in the actual implementation when required instead of absent-mindedly reverting to DateTime?
</description><link>http://ayende.com/3408/dealing-with-time-in-tests#comment1</link><guid>http://ayende.com/3408/dealing-with-time-in-tests#comment1</guid><pubDate>Mon, 07 Jul 2008 05:52:13 GMT</pubDate></item></channel></rss>