本文介绍了如何在不支持“PrivateObject"的 .Net Core 应用程序中对私有方法进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个 .NET Core 应用程序.

My application is a .NET Core application.

我有一个 public 方法,如下所示,它有两个私有方法.

I have a public method as shown below which has two private methods.

   public bool CallService(JObject requestJsonObj, out Status status)
   {
        bool provisioningSuccess = false;
        var preProcessSuccess = PreProcessing(requestJsonObj,out Status status);
        var postProcessSuccess = PostProcessing(requestJsonObj,out Status status);

        if(preProcessSuccess && postProcessSuccess)
        {
              status = Status.ProvisionSuccess;
              provisioningSuccess = true;
        }
        return provisioningSuccess;
   }

这里是状​​态和私人课程

Here is Status and private classes

   public enum Status
   {
       [Description("User/Pwd not valid")]
       CredentialInvalid = 111,
       [Description("Provision is success")]
       ProvisionSuccess = 112,
   }

    private PreProcessing(JObject JosnObj, 
        out Status status)
    {
           using (var client = new HttpClient())
           {
                var request = new {.........};
                var response = client.PostAsJsonAsync("api/preprocess", request).Result;
           }
    }

    private PostProcessing(JObject JosnObj, 
        out Status status)
    {
            //..... 
    }

尝试了以下方法,

     PrivateObject privateHelperObject = new PrivateObject(typeof(MainService));
     actual = (bool)privateHelperObject.Invoke("CallService", requestJsonObj,status);    

它说

找不到类型或命名空间名称PrivateObject"(您是否缺少 using 指令或程序集引用?)

这是一个 .NET CORE 项目.我不确定 .net 核心是否支持 PrivateObject?

This is a .NET CORE project. I am not sure if PrivateObject is supported .net core?

推荐答案

您首先不需要 PrivateObject,因为您要测试的成员是 public:

You don't need PrivateObject in the first place, as your member to test is public:

var target = new MainService();
var actual = target.CallService(requestJsonObj, status);

您的方法本身调用私有方法不会改变您测试 public 方法的方式.

That your method itself calls private method doesn't change how you test the public one.

如果你真的需要测试私有的,事情就会变得更难.所以让我们使用反射,这也是 PrivateObject 在幕后所做的.

Things get harder if you really need to test the private ones also. So let´s use reflection, which is what PrivateObject does as well under the hood.

var mainServiceObject = new MainService();
var method = mainService.GetType().GetMethod("PreProcessing", BindingFlags.Instance | BindingFlags.NonPublic);

var result = (bool) method.Invoke(mainServiceObject, new[] { requestJsonObj, status });

但是请注意,单元测试私有成员通常被认为是一种代码异味,并且通常表明设计存在问题 - 即您的类做得太多,应该分成多个类,每个类都有一个责任.

However be aware that unit-testing private members usually is considered a code smell and often indicates that there are issues with the design - namely that your class is doing too much and should be split into multiple classes each having a single responsibility.

这篇关于如何在不支持“PrivateObject"的 .Net Core 应用程序中对私有方法进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:01