Unit test WPF Bindings

I am trying to unit test my WPF databindings using the test suit provided by Microsoft Team System. I would like to be able to test the bindings without showing the window because most of my tests will be for user controls and not actually on a window. Is this possible or is there a better way to do it? The code below works if I show the window, but if I don't, the bindings don't update.

            Window1_Accessor target = new Window1_Accessor();
    		UnitTestingWPF.Window1_Accessor.Person p = new UnitTestingWPF.Window1_Accessor.Person() { FirstName = "Shane" };
    		Window1 window = (target.Target as Window1);
    		window.DataContext = p;			
    		//window.Show(); //Only Works when I actually show the window
    		//Is it possible to manually update the binding here, maybe?  Is there a better way?
    		Assert.AreEqual("Shane", target.textBoxFirstName.Text);  //Fails if I don't Show() the window because the bindings aren't updated

Answers


Shane, if what you're really worried about is a binding breaking silently, you should look at redirecting the binding traces to somewhere you can examine. I'd start here:

http://blogs.msdn.com/mikehillberg/archive/2006/09/14/WpfTraceSources.aspx

Other than that, I agree with Gishu that bindings aren't good candidates for unit testing, mainly due to the automagic going on that Gishu mentioned in the "Epilogue". Instead focus on making sure the underlying class behaves correctly.

Note, too, that you can get even more robust traces using the PresentationTraceSources class:

http://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources.aspx

Hope that helps!


While looking for a solution to convert WPF binding errors into exception, I figured out that it can also be used in a unit test project.

The technique is very simple:

  1. Derive a TraceListener that throws instead of logging
  2. Add that listener to PresentationTraceSources.DataBindingSource

Please see the complete solution on GitHub, it includes a unit test project.


Need Your Help

Insert hexadecimal values in array in MATLAB

matlab

I am very new to matlab. I want to store hexadecimal values in array like this

What does “performant” software actually mean?

performance terminology

I see it used a lot, but haven't seen a definition that makes complete sense.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.