7

私はまだ添付されたビヘイビアを一般的に理解していて、単体テストを書く方法を見て途方に暮れています。

以下に、Sacha Barber の Cinch フレームワークのコードをいくつか貼り付けました。これにより、添付された動作を介してウィンドウを閉じることができます。誰かが単体テストの例を見せてもらえますか?

ありがとう!
ベリル

    #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
4

2 に答える 2

5

ICommandaDelegateCommandまたは aを使用してラムダを使用する可能性があります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

于 2010-02-19T15:49:20.413 に答える
3

DependencyPropertyの変更と値の強制は、私にとっては「不可能な依存関係」のように見えます。そこでWindowを参照すると、事態はさらに複雑になります。ここではHumbleObjectパターンを使用すると思います...

于 2010-02-19T15:44:33.500 に答える