سؤال

ما زلت أتعامل مع السلوكيات المرفقة بشكل عام ، وأنا في حيرة لمعرفة كيفية كتابة اختبار الوحدة لأحد.

لقد لصق بعض التعليمات البرمجية أدناه من إطار عمل Sacha Barber الذي يسمح بإغلاق نافذة عن طريق السلوك المرفق. هل يمكن أن تُظهر لي اختبار وحدة مثال على ذلك؟

شكرًا!
بيري

    #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
هل كانت مفيدة؟

المحلول

من المحتمل أن تستخدم lambda في الخاص بك ICommand باستخدام DelegateCommand أو أ RelayCommand. توجد تطبيقات متعددة لهذه في كل مكان وقد يكون لدى Cinch شيئًا مشابهًا. إصدار بسيط حقًا (كمثال ، لا يعني استخدام الإنتاج):

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
}

ثم قد يبدو جسمك الاختبار شيئًا كهذا:

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

هذا مثال مبالغ فيه. هناك بالطبع اختبارات أخرى سترغب في القيام بها ، وفي هذه الحالة يجب عليك إنشاء أو العثور على تطبيق أكمل DelegateCommand هذا أيضا ينفذ بشكل صحيح CanExecute, ، ضمن أشياء أخرى.

نصائح أخرى

يبدو أن تغيير التبعية وإكراه القيمة بمفردهم يشبه "التبعيات المستحيلة" بالنسبة لي. الإشارة إلى النافذة هناك يجعل الأمور أكثر صعوبة. أعتقد أنني سأذهب مع نمط الكائن المتواضع هنا...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top