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?
pageContent
toIAddChild
and performed the operation directly, rather than relying on theToMaybeOf
andDo
:((IAddChild)pageContent).AddChild(fixedPage);
– Oceanid