systemtime

Unit Testing: DateTime.Now

喜你入骨 提交于 2019-11-26 00:57:54
问题 I have some unit tests that expects the \'current time\' to be different than DateTime.Now and I don\'t want to change the computer\'s time, obviously. What\'s the best strategy to achieve this? 回答1: The best strategy is to wrap the current time in an abstraction and inject that abstraction into the consumer. Alternatively , you can also define a time abstraction as an Ambient Context : public abstract class TimeProvider { private static TimeProvider current = DefaultTimeProvider.Instance;