Created
November 18, 2014 02:23
-
-
Save hatelove/bc2b3ee39d600fe59fe0 to your computer and use it in GitHub Desktop.
mock DateTime example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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