单元测试WPF绑定 [英] Unit test WPF Bindings

查看:296
本文介绍了单元测试WPF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行单元测试使用Microsoft团队系统提供的测试适合我的WPF数据绑定。我想能够测试绑定而不显示窗口,因为大多数的我的测试将是用户控制和不实际的窗口上。这是可能的,或者有没有更好的办法做到这一点?下面的code工作,如果我显示窗口,但如果我不这样做,绑定不更新。

  Window1_Accessor目标=新Window1_Accessor();
    UnitTestingWPF.Window1_Accessor.Person P =新UnitTestingWPF.Window1_Accessor.Person(){名字=谢恩};
    窗口1窗口=(target.Target为窗口1);
    window.DataContext = P;
    //window.Show(); //只工作当我真正显示窗口
    //是否可以手动更新这里的结合,也许?有没有更好的办法?
    Assert.AreEqual(谢恩,target.textBoxFirstName.Text); //失败,如果我不显示()的窗口,因为没有更新的绑定


解决方案

巴蒂尔,如果你真的很担心是有约束力的突破默默,你应该看看重定向结合痕迹,什么地方可以检查。我会从这里开始:

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

除此之外,我Gishu同意绑定,则不适合进行单元测试,主要是由于AUTOMAGIC正在进行中的后记中提到Gishu。相反,专注于确保基础类正确行为。

请注意,那就是,你可以使用presentationTraceSources类获得更强大的痕迹:

http://msdn.microsoft.com/en-us/library/system.diagnostics.$p$psentationtracesources.aspx

希望帮助!

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

解决方案

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!

这篇关于单元测试WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆