Question

Je suis toujours Grokking comportements attachés en général, et je suis à une perte pour voir comment écrire un test pour une unité.

Je collais un code ci-dessous du cadre Cordelière de Sacha Barber qui permet à une fenêtre fermée par le comportement ci-joint. Peut somewone me montrer un exemple test unitaire pour elle?

Merci!
Berryl

    #region Close

    /// <summary>Dependency property which holds the ICommand for the Close event</summary>
    public static readonly DependencyProperty CloseProperty =
        DependencyProperty.RegisterAttached("Close",
            typeof(ICommand), typeof(Lifetime),
                new UIPropertyMetadata(null, OnCloseEventInfoChanged));

    /// <summary>Attached Property getter to retrieve the CloseProperty ICommand</summary>
    public static ICommand GetClose(DependencyObject source)
    {
        return (ICommand)source.GetValue(CloseProperty);
    }

    /// <summary>Attached Property setter to change the CloseProperty ICommand</summary>
    public static void SetClose(DependencyObject source, ICommand command)
    {
        source.SetValue(CloseProperty, command);
    }

    /// <summary>This is the property changed handler for the Close property.</summary>
    private static void OnCloseEventInfoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var win = sender as Window;
        if (win == null) return;

        win.Closing -= OnWindowClosing;
        win.Closed -= OnWindowClosed;

        if (e.NewValue == null) return;

        win.Closing += OnWindowClosing;
        win.Closed += OnWindowClosed;
    }

    /// <summary>
    /// This method is invoked when the Window.Closing event is raised.  
    /// It checks with the ICommand.CanExecute handler
    /// and cancels the event if the handler returns false.
    /// </summary>
    private static void OnWindowClosing(object sender, CancelEventArgs e)
    {
        var dpo = (DependencyObject)sender;
        var ic = GetClose(dpo);
        if (ic == null) return;

        e.Cancel = !ic.CanExecute(GetCommandParameter(dpo));
    }

    /// <summary>
    /// This method is invoked when the Window.Closed event is raised.  
    /// It executes the ICommand.Execute handler.
    /// </summary>
    static void OnWindowClosed(object sender, EventArgs e)
    {
        var dpo = (DependencyObject)sender;
        var ic = GetClose(dpo);
        if (ic == null) return;

        ic.Execute(GetCommandParameter(dpo));
    }

    #endregion
Était-ce utile?

La solution

Vous utiliserez probablement un lambda dans votre ICommand en utilisant un DelegateCommand ou un RelayCommand. implémentations multiples de ceux-ci existent dans tous les sens et Cinch peuvent avoir quelque chose de similaire. Version très simple (comme un exemple, non destiné à un usage de la production):

public class DelegateCommand : ICommand {
    private Action _execute = null;

    public void Execute( object parameter ) {
        _execute();
    }

    public DelegateCommand( Action execute ) {
        _execute = execute;
    }

    #region stuff that doesn't affect functionality
    public bool CanExecute( object parameter ) {
        return true;
    }
    public event EventHandler CanExecuteChanged {
        add { }
        remove { }
    }
    #endregion
}

Ensuite, votre corps de test pourrait ressembler à ceci:

bool wascalled = false;

var execute = new DelegateCommand(
    () => {
        wascalled = true;
    } );

var window = new Window();
SomeClass.SetClose( window, execute );

// does the window need to be shown for Close() to work? Nope.

window.Close();

AssertIsTrue( wascalled );

Ceci est un exemple simpliste. Il y a bien sûr d'autres tests que vous aurez envie de réaliser, dans ce cas, vous devez créer ou trouver une application plus complète de DelegateCommand qui a également implémente correctement CanExecute, entre autres.

Autres conseils

DependencyProperty changement et la coercition de valeur sur leurs propres regards comme « Impossible » pour Dependencies moi. En référence à la fenêtre il rend les choses encore plus délicat. Je pense que je partirais avec Humble ici ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top