What's a good way to overwrite DateTime.Now during testing?

后端 未结 11 1184
一个人的身影
一个人的身影 2020-11-28 02:52

I\'ve got some (C#) code that relies on today\'s date to correctly calculate things in the future. If I use today\'s date in the testing, I have to repeat the calculation in

11条回答
  •  离开以前
    2020-11-28 03:24

    Simple answer: ditch System.DateTime :) Instead, use NodaTime and it's testing library: NodaTime.Testing.

    Further reading:

    • Unit testing with Noda Time
    • Dependency Injection: Inject Your Dependencies, Period!

提交回复
热议问题