单元测试如何覆盖internal的方法

发表于:2018-10-09 10:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:linkanyway    来源:51testing采编

  在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大
  对于private方法,那只有做一个副本然后改成internal或public来测试
  对于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 }


   上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号