Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some options for doing it:</p> <ol> <li><p>Use mocking framework and use a DateTimeService (Implement a small wrapper class and inject it to production code). The wrapper implementation will access DateTime and in the tests you'll be able to mock the wrapper class.</p></li> <li><p>Use <a href="http://site.typemock.com/typemock-isolator-product/" rel="noreferrer">Typemock Isolator</a>, it can <a href="http://blog.typemock.com/2009/05/mockingfaking-datetimenow-in-unit-tests.html" rel="noreferrer">fake DateTime.Now</a> and won't require you to change the code under test.</p></li> <li><p>Use <a href="http://research.microsoft.com/en-us/projects/moles/" rel="noreferrer">Moles</a>, it can also <a href="http://blog.dotnetwiki.org/2010/01/29/UsingMolesToTestDateTimeNowOnChannel9.aspx" rel="noreferrer">fake DateTime.Now</a> and won't require change in production code.</p></li> </ol> <p>Some examples:</p> <p><em>Wrapper class using Moq:</em></p> <pre><code>[Test] public void TestOfDateTime() { var mock = new Mock&lt;IDateTime&gt;(); mock.Setup(fake =&gt; fake.Now) .Returns(new DateTime(2000, 1, 1)); var result = new UnderTest(mock.Object).CalculateSomethingBasedOnDate(); } public class DateTimeWrapper : IDateTime { public DateTime Now { get { return DateTime.Now; } } } </code></pre> <p><em>Faking DateTime directly using Isolator:</em></p> <pre><code>[Test] public void TestOfDateTime() { Isolate.WhenCalled(() =&gt; DateTime.Now).WillReturn(new DateTime(2000, 1, 1)); var result = new UnderTest().CalculateSomethingBasedOnDate(); } </code></pre> <p><em>Disclaimer - I work at Typemock</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload