zoukankan      html  css  js  c++  java
  • 单元测试如何覆盖internal的方法

    在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大

    1. 对于private方法,那只有做一个副本然后改成internal或public来测试
    2. 对于internal的类和方法可以直接利用[assembly]标签来指定该类可以被哪个dll/方案读取
      1 /*******************************************************************************
      2 * File Name:  SoftwareInfoHelper.cs
      3 * Namespace: 
      4 *
      5 * CreateTime: 2018/9/6 14:50
      6 * Author:  linkanyway
      7 * Description: SoftwareInfoHelper
      8 * Class Name: SoftwareInfoHelper
      9 *
     10 * Ver ChangeDate Author Description
     11 * ───────────────────────────────────
     12 * V0.01 2018/9/6 14:50 linkanyway draft
     13 *
     14 * Copyright (c) 2018 linkanyway 
     15 * Description: Framework
     16 *
     17 *********************************************************************************/
     18 
     19 using System;
     20 using System.Reflection;
     21 using System.Runtime.CompilerServices;
     22 using System.Runtime.InteropServices;
     23 
     24 
     25 [assembly: InternalsVisibleTo("TestProject.Nirvana.Infrastructure")
     26 ]
     27 
     28 namespace Nirvana.Infrastructure.Runtime
     29 {
     30     /// <summary>
     31     /// SoftwareInfoHelper
     32     /// </summary>
     33     // ReSharper disable once UnusedMember.Global
     34     public static class SoftwareInfoHelper
     35     {
     36         /// <summary>
     37         /// Return correct .NET Core product name like ".NET Core 2.1.0" instead of ".NET Core 4.6.26515.07" returning by RuntimeInformation.FrameworkDescription
     38         /// </summary>
     39         /// <returns></returns>
     40         // ReSharper disable once UnusedMember.Global
     41         internal static string GetFrameworkDescription()
     42         {
     43             // ".NET Core 4.6.26515.07" => ".NET Core 2.1.0"
     44             // var parts = RuntimeInformation.FrameworkDescription.Split([' '], StringSplitOptions.RemoveEmptyEntries);
     45             var charSeparators = new[] {','};
     46             var parts = RuntimeInformation.FrameworkDescription.Split(charSeparators,
     47                 StringSplitOptions.RemoveEmptyEntries);
     48             var i = 0;
     49             for (; i < parts.Length; i++)
     50             {
     51                 if (char.IsDigit(parts[i][0]))
     52                 {
     53                     break;
     54                 }
     55             }
     56 
     57             var productName = string.Join("", parts, 0, i);
     58             return string.Join("", productName, " ", GetNetCoreVersion());
     59         }
     60 
     61 
     62         /// <summary>
     63         /// 
     64         /// </summary>
     65         /// <returns></returns>
     66         internal static string GetNetCoreVersion()
     67         {
     68             var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
     69             var assemblyPath = assembly.CodeBase.Split(new[] {'/', '\'}, StringSplitOptions.RemoveEmptyEntries);
     70             var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
     71             if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
     72                 return assemblyPath[netCoreAppIndex + 1];
     73             return null;
     74         }
     75 
     76 
     77         
     78         /// <summary>
     79         /// 
     80         /// </summary>
     81         /// <returns></returns>
     82         public static PlatformInformation GetPlatformInformation()
     83         {
     84             OSPlatform platform;
     85 
     86             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     87                 platform = OSPlatform.Windows;
     88             if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     89                 platform = OSPlatform.Linux;
     90             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     91                 platform = OSPlatform.OSX;
     92             return new PlatformInformation()
     93             {
     94                 Architecture = RuntimeInformation.OSArchitecture,
     95                 DotnetVersion = GetNetCoreVersion(),
     96                 Os = platform,
     97                 OsVersion = RuntimeInformation.OSDescription
     98                 
     99             };
    100         }
    101     }
    102 }
  • 相关阅读:
    短信
    solr测试用的配置
    中文词启动
    配置域
    applicationContext-redis.xml
    Redis端口配置
    springDataRedis 依赖
    FastDFSClient上传图片工具类
    security 页面测试
    PHP图片压缩功能(按比例图片缩放)(转载)
  • 原文地址:https://www.cnblogs.com/linkanyway/p/unittest-internal-visiable.html
Copyright © 2011-2022 走看看