C# Unit Testing(Nunit) the Main method of a console app?

后端 未结 1 1637
逝去的感伤
逝去的感伤 2021-02-07 13:08

I have a question on unit testing the Main method of a console app. The standard signature is

  public static void Main(string[] args)

I want t

相关标签:
1条回答
  • 2021-02-07 14:07

    There is nothing to mock in your scenario. Static Program.Main is a method just as any other and you test it as such -- by invoking it.

    The issue with static void method is that you can only verify whether it throws exception or interacts with input argument (or other static members, eventually). Since there is nothing to interact with on string[] you can test former case.

    However, a more sound approach is to delegate all logic contained in Main to separate component and test it instead. Not only this allows you to test your input argument handling logic thoroughly but also simplifies Main to more or less this:

    public static void Main(string[] args)
    {
        var bootstrapper = new Bootstrapper();
        bootstrapper.Start(args);
    }
    
    0 讨论(0)
提交回复
热议问题