我有一个“命令”类:
public static class MyCommands
{
private static ICommand exitCommand = new RoutedCommand();
public static ICommand ExitCommand { get { return exitCommand; } }
}
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();
}
以及实现菜单栏的用户控件中的一些 XAML:
<MenuItem Header="_Exit"
Command="{x:Static local:MyCommands.ExitCommand}"
/>
该代码有效。我喜欢所涉及的一般模式,我想继续使用它。
但是,我也在努力追求其他一些目标,例如进行测试驱动开发并通过我的单元和集成测试实现 100% 的覆盖率。我还希望 100% 符合 StyleCop 和 FxCop 警告。我被困在这里。
按照 FxCop (Microsoft.Security:CA2109) 的建议,我的MainWindow.Exit()
方法是私有的,但这意味着我不能直接从测试中调用它。我想我可以将其公开并禁止显示 FxCop 消息。或者我可以使用访问器。但是我倾向于不直接针对私有方法编写测试,尤其是在这种情况下,因为所做的只是测试方法而不是命令绑定本身。
我觉得必须有其他方法从我的测试代码中调用命令,以便我可以验证命令是否按预期工作(除了手动测试)。有什么建议么?