Question

I am trying to write a unit test for the following code:

public static void AppExitCmdCanExecute(object sender,
                                        CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

The problem with this code is that I am not able to create a mock instance (sealed class ) or instance (internal constructor) of CanExecuteRoutedEventArgs type.

I tried the following, but both the following code throw run-time exception.

[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);
}

Thanks for your interest.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top