Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created November 18, 2014 02:23
Show Gist options
  • Save hatelove/bc2b3ee39d600fe59fe0 to your computer and use it in GitHub Desktop.
Save hatelove/bc2b3ee39d600fe59fe0 to your computer and use it in GitHub Desktop.
mock DateTime example
[TestMethod]
public void TestMethod1()
{
//arrange
var p = new Person();
MockContext.MockNow = new DateTime(2011, 1, 1);
var result = p.Cal();
}
public class Person
{
internal long Cal()
{
//return DateTime.Now.Ticks;
return MockContext.MockNow.Ticks;
}
}
public static class MockContext
{
private static DateTime? _now;
public static DateTime MockNow
{
get
{
if (_now == null)
{
_now = DateTime.Now;
}
return (DateTime)_now;
}
set
{
_now = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment