Unit Testing: Logging and Dependency Injection

后端 未结 9 563
臣服心动
臣服心动 2021-02-02 14:57

So regards logging from SO and other sites on the Internet the best response seems to be:

void DoSomething() {
    Logger.Log(\"Doing something!\");
    // Code.         


        
9条回答
  •  不思量自难忘°
    2021-02-02 15:53

    I would probably have a separate body of unit tests for the logger itself, to test out its various functions separately from everything else. In methods that are using the logger, I would just test that the logger was invoked (i.e. expect that it was called) with the right parameters. For example, if I have a method:

    DoSomething()
    {
        if (FatalErrorOccurred)
        {
            Logger.Log("Fatal Error", ErrorLevel.Fatal);
        }
    }
    

    I would write a test that shows the logger logged a fatal error message when FatalErrorOccurred was true. I would not, of course, test the contents of the error message itself, as that is very susceptible to change.

提交回复
热议问题