我注意到,爲了在我的OO代碼中對每個單元進行單元測試,我需要將訪問修飾符設置爲public,即使應該保護或可能是私有的方法。這可以練習嗎?OO中的單元測試和訪問修飾符
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
}
可能不是。爲什麼你需要直接測試私有方法(即不屬於你的API的方法)?如果它們足夠複雜以至於需要單獨測試,那麼可能有一種觀點認爲應將此行爲分解爲一個單獨的類別...... –
通過發佈您已在代碼中完成的內容,可以改進此問題。 –