OO中的单元测试和访问修饰符

Kob*_*kie 3 .net c# c++ nunit unit-testing

我注意到为了在我的OO代码中对每个单元进行单元测试,我需要将访问修饰符设置为public,即使是应该受到保护的方法,也可能是私有的.这可以吗?

public class EnforceBusinessRules
{
    BusinessState m_state;

    public EnforceBusinessRules()
    {
        m_state = START;
    }


    public bool isInputcurrentlyFormatted(string input)
    {
        //code goes here to ensure the input passes formatting test
        //modify m_state appropriately
    }


    public bool InputContainsValidStartAndEndTokens(string input)
    {
        //code goes here to ensure that the start and end tokens of the input are of the type available in the system
        //modify m_state appropriately
    }


    public bool StartEndCommandisValidAccordingtoCurrentSystemSettings(string input)
    {
        //code goes here to check the start and End codes match the current start and end codes for the day
        //modify m_state appropriately
    }

    // and so on 
}
Run Code Online (Sandbox Code Playgroud)

Lia*_*iam 5

单元测试是"黑盒子"测试.您应该只测试外部可见元素.如果您测试所有内部工作,那么在不修改所有单元测试的情况下,您无法正确重构代码.

  • 实际上,单元测试是白盒测试.验收测试是黑盒测试.但是,在这两种情况下,您只测试可见(公共)API. (2认同)