Mocking Static Methods

前端 未结 6 2191
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 15:11

Recently, I\'ve begun to use Moq to unit test. I use Moq to mock out classes that I don\'t need to test.

How do you typically deal with static methods?



        
6条回答
  •  囚心锁ツ
    2020-11-27 15:24

    I liked Pose but couldn't get it to stop throwing InvalidProgramException which appears to be a known issue. Now I'm using Smocks like this:

    Smock.Run(context =>
    {
        context.Setup(() => DateTime.Now).Returns(new DateTime(2000, 1, 1));
    
        // Outputs "2000"
        Console.WriteLine(DateTime.Now.Year);
    });
    

提交回复
热议问题