mli*_*bby 2 c# testing wpf commandbinding
我有一个"命令"类:
public static class MyCommands
{
private static ICommand exitCommand = new RoutedCommand();
public static ICommand ExitCommand { get { return exitCommand; } }
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
实现菜单栏的用户控件中的一些XAML:
<MenuItem Header="_Exit"
Command="{x:Static local:MyCommands.ExitCommand}"
/>
Run Code Online (Sandbox Code Playgroud)
代码有效.我喜欢所涉及的一般模式,我想继续使用它.
但是,我也在努力追求其他目标,例如进行测试驱动开发,并通过我的单元和集成测试实现100%的覆盖率.我还希望100%符合StyleCop和FxCop警告.而且我被抓到了这里.
我的MainWindow.Exit()方法是私有的,正如FxCop(Microsoft.Security:CA2109)所推荐的那样,但这意味着我不能直接从测试中调用它.我想我可以把它公开并压制FxCop消息.或者我可以使用访问者.但是我反对直接针对私有方法编写测试,特别是在这种情况下,因为所有这些都是测试方法而不是命令绑定本身.
我觉得必须有其他方法从我的测试代码调用命令,以便我可以验证命令是否按预期工作(除了手动测试).有什么建议?
我意识到这是一个古老的问题,但我想我会回答,以防它帮助其他人.
您可以使用以下命令从代码隐藏中调用命令:
ICommand command = ExitCommand;
command.Execute();
Run Code Online (Sandbox Code Playgroud)
这将执行Exit()并且不需要访问者.这是你在找什么?
| 归档时间: |
|
| 查看次数: |
3263 次 |
| 最近记录: |