WPF MVVM - Unit Testing a command - Private vs Public methods?



Answers

Personally, I'd go with private methods and I'll tell you why. You're exposing an ICommand, which to me says that the consuming view should call a CanExecute prior to calling an Execute. If they don't, they're going against the API and shooting themselves in the foot and at that point it is out of your hands. Just like if someone used reflection to set an important private variable to null and broke your class design because of this... shooting themselves in the foot. So why make the members private? Because there is no need to expose members that should not be called directly.


Basically, when you unit test the members you don't do so individually, you do so in the way the API intends for the members to be executed. So, you're not really testing the members, but moreover you're testing the command, which again means they should be tested in a pair in the specific order of:

if (CanExecute)
{
    Execute;
}
Question

Basically, If I use MVVM and expose public ICommands, should my delegates be public or private?






Links



Tags