C 非public的方法和属性的单元测试

C 非public的方法和属性的单元测试有时候我们写好的类库中 某些类的属性和方法不应该暴露出来 那么如何对这些非 public 的方法和属性进行单元测试 MS 为我们提供了 PrivateObjec 类 可以解决这个问题 可以去 MSDN 的说明文档中查看这个类的介绍 本文也试举一例 说明其用法 首先给出被测试类

大家好,我是讯享网,很高兴认识大家。

有时候我们写好的类库中,某些类的属性和方法不应该暴露出来,那么如何对这些非public的方法和属性进行单元测试?

MS为我们提供了PrivateObject类,可以解决这个问题,可以去MSDN的说明文档中查看这个类的介绍。本文也试举一例,说明其用法。


讯享网

首先给出被测试类,为了比较全面,我们在其中包含了public属性、protected属性、private属性等等,如下

 /// <summary> /// Implementation of a class with non-public members. /// </summary> public class Testee { #region Public Properties /// <summary> /// Gets / sets the value of a public property. /// </summary> public string PublicProperty { get; set; } #endregion Public Properties #region Protected Properties /// <summary> /// Gets / sets the value of a protected property. /// </summary> protected string ProtectedProperty { get; set; } #endregion Protected Properties #region Private Properties /// <summary> /// Gets / sets the value of a private property. /// </summary> private string PrivateProperty { get; set; } #endregion Private Properties #region Private Static Properties /// <summary> /// Gets / sets the value of a private static property. /// </summary> private static string PrivateStaticProperty { get; set; } #endregion Private Static Properties #region Protected Methods /// <summary> /// A simple protected method with a parameter. /// </summary> /// <param name="parameter">The parameter</param> /// <returns>Another string.</returns> protected string ProtectedMethod(string parameter) { Console.WriteLine("Parameter: >{0}<", parameter); return (parameter + " processed"); } #endregion Protected Methods }

讯享网
小讯
上一篇 2025-02-20 11:00
下一篇 2025-03-26 20:07

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/125374.html