Domanda

Sto cercando di scrivere un test unitario per il seguente codice:

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

Il problema con questo codice è che non sono in grado di creare un'istanza simulata (classe sigillata) o istanza (costruttore interno) del tipo CANEXECUTEROUTEDEventArgs.

Ho provato quanto segue, ma entrambe le seguenti eccezioni di runtime di lancio del codice.

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

Grazie per il tuo interesse.

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top