How can I test private methods with DUnit?

后端 未结 7 648
温柔的废话
温柔的废话 2021-01-01 17:41

I have a class that I am unit testing into with DUnit. It has a number of methods some public methods and private methods.

type
  TAuth = class(TDataModule)
         


        
7条回答
  •  迷失自我
    2021-01-01 18:09

    I recommend the "XUnit Test Patterns" book by Gerard Meszaros:

    Test-Specific Subclass

    Question: How can we make code testable when we need to access private state of the SUT?

    Answer: Add methods that expose the state or behavior needed by the test to a subclass of the SUT.

    ... If the system under test (SUT) was not designed specifically to be testable, we may find that the test cannot get access to state that it must initialize or verify at some point in the test.

    The article also explains when to use it and which risks it carries.

提交回复
热议问题