ApprovalTests.Wpf

November 20, 2019 ยท View on GitHub

Extends ApprovalTests for approval of WPF through screenshot verification.

The NuGet package NuGet Status

https://nuget.org/packages/ApprovalTests.Wpf/

PM> Install-Package ApprovalTests.Wpf

Usage

Given the following binding model:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    string myProperty;

    public string MyProperty
    {
        get => myProperty;
        set
        {
            myProperty = value;
            RaisePropertyChanged();
        }
    }

    void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

snippet source / anchor

BindsWithoutError

The bindings can be verified using the following:

var viewModel = new ViewModel();
var myBinding = new Binding(nameof(ViewModel.MyProperty))
{
    Source = viewModel
};
var exception = ExceptionUtilities.GetException(
    () => WpfBindingsAssert.BindsWithoutError(viewModel,
        () =>
        {
            var textBox = new TextBox();
            textBox.SetBinding(TextBox.TextProperty, myBinding);
            return textBox;
        }));
Assert.Null(exception);

snippet source / anchor