All right, do not get angry now, I know there are several questions about this topic, but I still have some doubts.
I think I totally agree about not testing private functions, I find it reasonable, but how can I test public methods that set private variables?
Let’s say that the constructor set some private variables and I want to test that those variable are correctly set after the constructor is called. Is it a valid test? How should I check the value of the private variables without adding public getters?
I add a not real scenario example to try to be clearer:
public class ClassToTest
{
private bool _isOn;
public void SwitchOn() { _isOn = true; }
public void SwitchOff(){ _isOn = false; }
public void update()
{
if (_isOn)
DoSomething();
}
private void DoSomething()
{
//this could also execute a function of an external dependency. But still the dependency could not have a public function to test if the behavior actually ran.
}
}
how can I test that SwitchOn and SwitchOff work properly if I cannot test against the _isOn value? (this is an example, it implies that I will not write a public getter and the functions do not return a value because they do not need to)
Tests should use the Assemble/Activate/Assert pattern:
You assert the actual reason you have a switch. Testing the switch itself breaks the rule “Don’t TDD getters and setters”.