I have a "commands" class:
public static class MyCommands
{
private static ICommand exitCommand = new RoutedCommand();
public static ICommand ExitCommand { get { return exitCommand; } }
}
Code-behind in MainWindow.xaml.cs:
private void BindCommands()
{
this.CommandBindings.Add(new CommandBinding(MyCommands.ExitCommand, this.Exit));
}
private void Exit(object sender, ExecutedRoutedEventArgs e)
{
Application.Current.Shutdown();
}
And some XAML in a user control that implements a menu bar:
<MenuItem Header="_Exit"
Command="{x:Static local:MyCommands.ExitCommand}"
/>
The code works. I like the general pattern involved and I'd like to continue using it.
However, I am also trying to pursue some other goals, such as doing Test Driven Development and achieving 100% coverage with my unit and integration tests. I'd also like to have 100% conformance with StyleCop and FxCop warnings. And I'm caught here.
My MainWindow.Exit()
method is private, as recommended by FxCop (Microsoft.Security:CA2109), but this means I can't call it directly from a test. I suppose I could make it public and suppress the FxCop message. Or I can use an accessor. But I have a bias against writing tests directly against private methods, especially in this case since all that does is test the method and not the command binding itself.
I feel like there must be some other way to invoke the command from my test code so that I can verify that the command works as intended (besides testing manually). Any suggestions?