我正在尝试为以下代码编写单元测试:
public static void AppExitCmdCanExecute(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
这段代码的问题是我无法创建 CanExecuteRoutedEventArgs 类型的模拟实例(密封类)或实例(内部构造函数)。
我尝试了以下方法,但以下代码均引发运行时异常。
[Test()]
public void AppExitCmdCanExecuteTest()
{
object sender = null;
//Type to mock must be an interface or an abstract or non-sealed class.
var mockArgs = new Moq.Mock<CanExecuteRoutedEventArgs>();
AppCommands.AppExitCmdCanExecute(sender, mockArgs.Object);
Assert.IsTrue(mockArgs.CanExecute);
}
[Test()]
public void AppExitCmdCanExecuteTest()
{
object sender = null;
//Constructor on type 'System.Windows.Input.CanExecuteRoutedEventArgs'
// not found.
var mockArgs = Activator.CreateInstance(typeof (CanExecuteRoutedEventArgs),
BindingFlags.NonPublic |
BindingFlags.Instance,
new object[2] {fakeCommand,
fakeParameter});
AppCommands.AppExitCmdCanExecute(sender, mockArgs);
Assert.IsTrue(mockArgs.CanExecute);
}
感谢您的关注。