ユニットテスト取り付け挙動WPF
-
21-09-2019 - |
質問
私はまだ1用のユニットテストを書く方法を確認するために損失で、一般的に取り付けた行動をgrokking、と思いますしています。
私は窓を取り付けた行動を経由して閉じることができサシャ理容室のシンチフレームワークから下にいくつかのコードを貼り付けました。それのために私の例でユニットテストを示しsomewoneすることはできますか?
ありがとう!
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
解決
あなたはおそらくICommand
またはDelegateCommand
を使用してRelayCommand
にラムダを使用します。これらの複数の実装は、あらゆる場所に存在し、シンチは似た何かを持っていることがあります。本当に簡単なバージョン(例として、本番環境で使用するためのものではない):
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
のより完全な実装を作成したり、見つける必要があり、その場合には、あなたが実行したいと思うもちろん他のテスト、であります。
他のヒント
DependencyPropertyに私のために「インポッシブル依存関係」のような、自分のルックスに変更し、値を強制。ウィンドウへの参照を持つものもトリッキーあります。私はここにハンブルオブジェクトパターンで...
で行くと思います所属していません StackOverflow