单元测试如何覆盖internal的方法
2024-08-25 16:17:27
在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大
- 对于private方法,那只有做一个副本然后改成internal或public来测试
- 对于internal的类和方法可以直接利用[assembly]标签来指定该类可以被哪个dll/方案读取
/*******************************************************************************
* File Name: SoftwareInfoHelper.cs
* Namespace:
*
* CreateTime: 2018/9/6 14:50
* Author: linkanyway
* Description: SoftwareInfoHelper
* Class Name: SoftwareInfoHelper
*
* Ver ChangeDate Author Description
* ───────────────────────────────────
* V0.01 2018/9/6 14:50 linkanyway draft
*
* Copyright (c) 2018 linkanyway
* Description: Framework
*
*********************************************************************************/ using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("TestProject.Nirvana.Infrastructure")
] namespace Nirvana.Infrastructure.Runtime
{
/// <summary>
/// SoftwareInfoHelper
/// </summary>
// ReSharper disable once UnusedMember.Global
public static class SoftwareInfoHelper
{
/// <summary>
/// Return correct .NET Core product name like ".NET Core 2.1.0" instead of ".NET Core 4.6.26515.07" returning by RuntimeInformation.FrameworkDescription
/// </summary>
/// <returns></returns>
// ReSharper disable once UnusedMember.Global
internal static string GetFrameworkDescription()
{
// ".NET Core 4.6.26515.07" => ".NET Core 2.1.0"
// var parts = RuntimeInformation.FrameworkDescription.Split([' '], StringSplitOptions.RemoveEmptyEntries);
var charSeparators = new[] {','};
var parts = RuntimeInformation.FrameworkDescription.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
var i = ;
for (; i < parts.Length; i++)
{
if (char.IsDigit(parts[i][]))
{
break;
}
} var productName = string.Join("", parts, , i);
return string.Join("", productName, " ", GetNetCoreVersion());
} /// <summary>
///
/// </summary>
/// <returns></returns>
internal static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] {'/', '\\'}, StringSplitOptions.RemoveEmptyEntries);
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > && netCoreAppIndex < assemblyPath.Length - )
return assemblyPath[netCoreAppIndex + ];
return null;
} /// <summary>
///
/// </summary>
/// <returns></returns>
public static PlatformInformation GetPlatformInformation()
{
OSPlatform platform; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
platform = OSPlatform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
platform = OSPlatform.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
platform = OSPlatform.OSX;
return new PlatformInformation()
{
Architecture = RuntimeInformation.OSArchitecture,
DotnetVersion = GetNetCoreVersion(),
Os = platform,
OsVersion = RuntimeInformation.OSDescription };
}
}
}
最新文章
- .NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2
- 小白如何进入IOS,答案就在这里
- 下载安装resin-3.X服务器并配置到myeclipse
- HDU 3555 数位dp入门
- hadoop2 作业执行过程之作业提交
- Android Mms 接收信息流程
- 面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- Redis的探究
- 空格&;amp;nbsp在不同浏览器中显示距离不一致问题解决方法
- mysql 自己定义存储过程和触发器
- 【百度地图API】如何给自定义覆盖物添加事件
- [html5] 学习笔记-编辑 API 之 Range 对象(一)
- PHP 页面静态化/纯静态化/伪静态化
- MySQL之爱之初体验
- Solr 06 - Solr中配置使用IK分词器 (配置schema.xml)
- __c语言__结构体、共用体、枚举__笔记
- Laravel 执行流程(一)之自动加载
- Windows Server 2008 R2终端服务器激活方法
- A1104. Sum of Number Segments
- 319. Bulb Switcher