Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Last active December 29, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayBazuzi/7670029 to your computer and use it in GitHub Desktop.
Save JayBazuzi/7670029 to your computer and use it in GitHub Desktop.
Use this to write a unit test that asserts that your XAML correctly binds to your ViewModel.
class AssertNoBindingErrorsTraceListener : TraceListener
{
readonly StringBuilder messageBuilder = new StringBuilder();
public AssertNoBindingErrorsTraceListener(SourceLevels level)
{
PresentationTraceSources.DataBindingSource.Listeners.Add(this);
PresentationTraceSources.DataBindingSource.Switch.Level = level;
}
public static IDisposable Start(SourceLevels level = SourceLevels.Warning)
{
var listener = new AssertNoBindingErrorsTraceListener(level);
return new DisposableToken(listener);
}
class DisposableToken : IDisposable
{
private readonly AssertNoBindingErrorsTraceListener _listener;
public DisposableToken(AssertNoBindingErrorsTraceListener listener)
{
_listener = listener;
}
public void Dispose()
{
string message = this._listener.messageBuilder.ToString();
Assert.True(string.IsNullOrEmpty(message), message);
this._listener.Flush();
this._listener.Close();
PresentationTraceSources.DataBindingSource.Listeners.Remove(this._listener);
}
}
public override void Write(string message)
{
messageBuilder.Append(message);
}
public override void WriteLine(string message)
{
messageBuilder.Append(message);
}
}
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Xunit;
// Requires
// HKCU\Software\Microsoft\Tracing\WPF
// ManagedTracing = (DWORD) 1
//
// TODO: Fail if that key is not set
static class WpfAssert
{
/// <summary>
/// Assert that the UserControl successfully binds to the given ViewModel
/// </summary>
public static void BindsSuccessfully(UserControl userControl, object viewModel)
{
var hostWindow = new Window { Content = userControl };
BindsSuccessfully(hostWindow, viewModel);
}
/// <summary>
/// Assert that the Window successfully binds to the given ViewModel
/// </summary>
public static void BindsSuccessfully(Window window, object viewModel)
{
using (AssertNoBindingErrorsTraceListener.Start())
{
window.DataContext = viewModel;
window.Show(); // force binding
}
}
}
public class WpfAssertTests
{
class TestViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public const string MyPropertyPropertyName = "MyProperty";
string _myProperty;
public string MyProperty
{
get { return this._myProperty; }
set
{
this._myProperty = value;
RaisePropertyChanged();
}
}
void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
[Fact]
public void Fail()
{
var viewModel = new TestViewModel();
Binding myBinding = new Binding(TestViewModel.MyPropertyPropertyName + "BOGUS") { Source = viewModel };
Assert.Throws<Xunit.Sdk.TrueException>(() =>
{
using (AssertNoBindingErrorsTraceListener.Start())
{
var textBox = new TextBox();
textBox.SetBinding(TextBlock.TextProperty, myBinding);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment