public class FluentBindingTest { protected readonly List ShouldBeBoundProperties = new List(); protected Window WpfWindow; protected UserControl WpfUserControl; public BindingTest BindingTest { get; private set; } public FluentBindingTest() { BindingTest = new BindingTest(); } public FluentBindingTest OnWindow(Window window) { if (WpfWindow != null) throw new InvalidOperationException("Window has already been set. A FluentBindingTest works on a single Window. A call to OnUserControl will set the Window as well."); WpfWindow = window; return this; } public FluentBindingTest OnUserControl(UserControl userControl) { if (!(WpfWindow == null && WpfUserControl==null)) throw new InvalidOperationException("Window has already been set. A FluentBindingTest works on a single Window. A call to OnUserControl will set the Window as well."); WpfWindow = new Window {Content = userControl}; WpfUserControl = userControl; return this; } public FluentBindingTest ShouldContainBindingForProperty(string boundPropertyName) { ShouldBeBoundProperties.Add(boundPropertyName); return this; } Assert.Fail(bindingFailureMessage.ToString()); } public void Validate() { const string crlf = "\r\n"; var bindingFailureMessage = new StringBuilder(); WpfWindow.Show(); try { BindingTest.OnFrameWorkElement(WpfWindow); foreach (var shouldBeBoundProperty in ShouldBeBoundProperties) { var result = BindingTest.ForProperty(shouldBeBoundProperty); if (result.BindingMatchType!=BindingMatchTypes.ExactMatch) bindingFailureMessage.Append(result.Message + crlf); } } finally { WpfWindow.Close(); } if (bindingFailureMessage.Length <= 0) return; bindingFailureMessage.Append("Have you checked that you are using the correct TBoundObjectType for this Window or UserControl"); } }
[RequiresSTA] [Test] public void Test_Validate_WhenCalledOnUserControlWithCorrectSimpleBindings_ShouldValidate() { //---------------Set up test pack------------------- var fluentBindingTest = new FluentBindingTest(); var part = new ComputerPart { Description = "Seagate 500Gb HDD", PartCode = "HDD001", Price = 1250, Stock = 10, Cost = 950 }; part.Save(); var partWindow = new WindowWithCorrectBindings {DataContext = part}; //---------------Execute Test ---------------------- fluentBindingTest.OnWindow(partWindow) .ShouldContainBindingForProperty("Description") .ShouldContainBindingForProperty("Price") .ShouldContainBindingForProperty("Stock") .ShouldContainBindingForProperty("Cost") //---------------Test Result ----------------------- .Validate(); }
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown();
This website is best viewed in Internet Explorer 7 & 8; Firefox 3.6.11; Opera 10.63; & Safari 4.