如何测试私有 Private/Internal 方法

2025-11-01 06:58:12

1、添加测试方法 TestMethod

2、初始化 PrivateObject(测试实例方法),PrivateType ( 测试私有方法)

[TestMethod]

       public void TestPrivateAdd()

       {

           PrivateObject po = new PrivateObject(new Calculate());

           Assert.AreEqual(po.Invoke("Add", 1, 2), 3);

       }

      

3、针对Internal修饰的一样可以使用 PrivateObject/PrivateType

  [TestMethod]

       public void TestInternalStaticAdd()

       {

           PrivateType po = new PrivateType(typeof(Calculate));

           Assert.AreEqual(po.InvokeStatic("AddStatic", 1, 2), 3);

       }

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢