Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:03:25+00:00 2026-05-18T05:03:25+00:00

I have this static helper function: public static DependencyObject GetParentObject(DependencyObject child) { if (child

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:03:26+00:00Added an answer on May 18, 2026 at 5:03 am

    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.

    public class Foo
    {
        static IVisualTreeHelper visualTreeHelper;
    
        static Foo()
        {
            Foo.visualTreeHelper = new FrameworkVisualTreeHelper();
        }
    
        public Foo(IVisualTreeHelper visualTreeHelper)
        {
            Foo.visualTreeHelper = 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 the IVisualTreeHelper
           return visualTreeHelper.GetParent(child);
       }
    }
    
    public interface IVisualTreeHelper
    {
        DependencyObject GetParent(DependencyObject reference);
    }
    
    public class FrameworkVisualTreeHelper : IVisualTreeHelper
    {
        public DependencyObject GetParent(DependencyObject reference)
        {
            return VisualTreeHelper.GetParent(reference);
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: public static string Join(this IEnumerable<string> strings, string separator) { return
I have a function that I use to add vectors, like this: public static
I've got this function public static AdoEntity.Inspector GetInspectorWithInclude(int id, List<string> properties) { using (var
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have a class like this: public class Stretcher : Panel { public static
I have a custom HtmlHelper; public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave
I'm using MVC3 with Razor. I have the following helper: public static class ImageActionLinkHelper
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
Does anyone have this Regular Expression? public static class HtmlAttributeValidator{ const bool REGEX_OPTIONS =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.