How do you unit test private methods?

前端 未结 30 2024
无人及你
无人及你 2020-11-22 06:44

I\'m building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it coul

30条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 07:38

    MS Test has a nice feature built in that makes private members and methods available in the project by creating a file called VSCodeGenAccessors

    [System.Diagnostics.DebuggerStepThrough()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TestTools.UnitTestGeneration", "1.0.0.0")]
        internal class BaseAccessor
        {
    
            protected Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject m_privateObject;
    
            protected BaseAccessor(object target, Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType type)
            {
                m_privateObject = new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(target, type);
            }
    
            protected BaseAccessor(Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType type)
                :
                    this(null, type)
            {
            }
    
            internal virtual object Target
            {
                get
                {
                    return m_privateObject.Target;
                }
            }
    
            public override string ToString()
            {
                return this.Target.ToString();
            }
    
            public override bool Equals(object obj)
            {
                if (typeof(BaseAccessor).IsInstanceOfType(obj))
                {
                    obj = ((BaseAccessor)(obj)).Target;
                }
                return this.Target.Equals(obj);
            }
    
            public override int GetHashCode()
            {
                return this.Target.GetHashCode();
            }
        }
    

    With classes that derive from BaseAccessor

    such as

    [System.Diagnostics.DebuggerStepThrough()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TestTools.UnitTestGeneration", "1.0.0.0")]
    internal class SomeClassAccessor : BaseAccessor
    {
    
        protected static Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType m_privateType = new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType(typeof(global::Namespace.SomeClass));
    
        internal SomeClassAccessor(global::Namespace.Someclass target)
            : base(target, m_privateType)
        {
        }
    
        internal static string STATIC_STRING
        {
            get
            {
                string ret = ((string)(m_privateType.GetStaticField("STATIC_STRING")));
                return ret;
            }
            set
            {
                m_privateType.SetStaticField("STATIC_STRING", value);
            }
        }
    
        internal int memberVar    {
            get
            {
                int ret = ((int)(m_privateObject.GetField("memberVar")));
                return ret;
            }
            set
            {
                m_privateObject.SetField("memberVar", value);
            }
        }
    
        internal int PrivateMethodName(int paramName)
        {
            object[] args = new object[] {
                paramName};
            int ret = (int)(m_privateObject.Invoke("PrivateMethodName", new System.Type[] {
                    typeof(int)}, args)));
            return ret;
        }
    

提交回复
热议问题