如何对使用 VisualTreeHelper 的内容进行单元测试?
我有这个静态辅助函数:
public static DependencyObject GetParentObject(DependencyObject child)
{
if (child == null) return null;
ContentElement contentElement = child as ContentElement;
if (contentElement != null)
{
var parent = ContentOperations.GetParent(contentElement);
if (parent != null) return parent;
var fce = contentElement as FrameworkContentElement;
return fce != null ? fce.Parent : null;
}
//if it's not a ContentElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}
它在真实的应用程序中工作,但我正在尝试为其编写一些单元测试。这是我的第一次尝试:
[Test]
public void GetParentObject_returns_immediate_parent()
{
var contentControl = new ContentControl();
var textBox = new TextBox();
contentControl.BeginInit();
contentControl.Content = textBox;
contentControl.EndInit();
var result = UIHelper.GetParentObject(textBox);
Assert.AreSame(contentControl, result);
}
不幸的是,它失败了,因为 VisualTreeHelper 返回 null。我如何模拟一个可行的视觉树?
I have this static helper function:
public static DependencyObject GetParentObject(DependencyObject child)
{
if (child == null) return null;
ContentElement contentElement = child as ContentElement;
if (contentElement != null)
{
var parent = ContentOperations.GetParent(contentElement);
if (parent != null) return parent;
var fce = contentElement as FrameworkContentElement;
return fce != null ? fce.Parent : null;
}
//if it's not a ContentElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}
It works in a real application, but I'm trying to write some unit tests for it. Here's my first attempt:
[Test]
public void GetParentObject_returns_immediate_parent()
{
var contentControl = new ContentControl();
var textBox = new TextBox();
contentControl.BeginInit();
contentControl.Content = textBox;
contentControl.EndInit();
var result = UIHelper.GetParentObject(textBox);
Assert.AreSame(contentControl, result);
}
Unfortunately it fails because VisualTreeHelper
is returning null. How can I mock up a visual tree that will work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于这里的答案 通过 Wpf-controls 打印文档并转换对于 XPS 我想出了以下扩展方法来创建可视化树。它在 NUnit 中运行良好,无需 STA 线程或其他任何东西。
请注意,
ToMaybeOf
内容基本上意味着将pageContent
视为IAddChild
并执行操作该接口Based on this answer here on printing documents via Wpf-controls and convert to XPS I came up with the following extension method to create the visual tree. It works well within NUnit without STA-thread or anything.
Please note that
ToMaybeOf
stuff basically means to treatpageContent
asIAddChild
and do an action on that interface这就是静力学存在问题的原因。
您可以抽象接口背后的功能并创建使用静态方法的默认实现。然后,您可以使用依赖项注入,这使得此单元测试变得微不足道 - 模拟对 IVisualTreeHelper 的依赖项或滚动您自己的存根实现,您可以将其配置为返回您分配的任何值。
显然,如果您在其他地方使用其他方法,则可能需要将其他
VisualTreeHelper
方法添加到您的接口和默认实现中。它仍然不完全干净,因为您正在测试的单元本身是静态的,当您尝试对依赖于 UIHelper 类的静态方法的任何类进行单元测试时,您将遇到完全相同的问题。
This is why statics are problematic.
You can abstract the functionality behind an interface and create a default implementation that uses the static method. You can then use dependency injection, which makes this unit test trivial -- mock the dependency on IVisualTreeHelper or roll your own stub implementation that you can configure to return any value you assign.
Obviously, you may need to add other
VisualTreeHelper
methods to your interface and default implementation, if you're using other methods elsewhere.It is still not completely clean because the unit you're testing is itself static, and you're going to run into exactly the same problem when you try to unit test any class that relies on your UIHelper class' static methods.
要模拟可视化树,您必须实际创建并渲染一棵可视化树。因此,您必须创建一个实际的窗口,这对于单元测试来说并不是特别理想。
To mock a visual tree you will have to actually create and render one. So you will have to create an actual window, wich isn't particulary ideal for a unit test.