gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:29:10 28 4
gpt4 key购买 nike

我注意到,为了对我的 OO 代码中的每个单元进行单元测试,我需要将访问修饰符设置为 public,即使在应该 protected 或可能是私有(private)的方法上也是如此。这是正确的做法吗?

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
}

最佳答案

单元测试是“黑盒”测试。您应该只测试外部可见的元素。如果您测试所有内部工作原理,那么如果不修改所有单元测试就无法正确重构代码。

关于c# - OO 中的单元测试和访问修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10870449/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com