gpt4 book ai didi

c# - 重写 OS 查询方法以变得可测试

转载 作者:行者123 更新时间:2023-11-30 17:05:50 24 4
gpt4 key购买 nike

我正在检查我们应用程序的单元测试并改进/添加更多单元测试。我在单元测试/测试驱动开发方面相当(不,非常)新手,我发现了我想要测试的以下方法。我被卡住了,我的问题是是否有办法重写它以使其可测试?

public static bool Is32BitOS()
{
string os = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
select x.GetPropertyValue("Caption")).First().ToString().Trim();

if (os.Equals("Microsoft Windows XP Professional"))
{
return true;
}

if (os.StartsWith("Microsoft Windows 7"))
{
string architecture = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
select x.GetPropertyValue("OSArchitecture")).First().ToString();
if (architecture == "64-bit")
{
return false;
}
}

return true;
}

最佳答案

您的方法有 3 个职责:

  1. 管理对象搜索器的创建
  2. 操作系统和架构检索
  3. 操作系统验证

为了启用测试,我将在 AssemblyInfo.cs 中添加类似于以下的一行:

[assembly: InternalsVisibleTo("Your.Test.Project")]

这样 protected 方法将对测试项目可见,但不能免费提供给客户。然后我会重写您的方法,以便将操作系统验证分开:

//Acceptance test this method
public static bool Is32BitOS()
{
var managementObjects = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>();

string os = (from x in managementObjects select x.GetPropertyValue("Caption")).First().ToString().Trim();
string architecture = (from x in managementObjects select x.GetPropertyValue("OSArchitecture")).First().ToString();

return Is32BitOS(os, architecture);
}

//Unit test this method
internal static bool Is32BitOS(string os, string architecture)
{
if (os.Equals("Microsoft Windows XP Professional"))
{
return true;
}

if (os.StartsWith("Microsoft Windows 7"))
{
string architecture = archRetriever();
if (architecture == "64-bit")
{
return false;
}
}
return true;
}

既然我们已经分离了关注点,我会说您的单元测试应该只验证 Is32BitOs 行为,而验收测试应该验证端到端堆栈。事实上,你在单元测试中没有什么值(value)

(from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
select x.GetPropertyValue("Caption")).First().ToString().Trim();

检索操作系统字符串,而您的真正值(value)在于使用此信息来确定操作系统是否为 32 位。

将事物包装在接口(interface)中并使用模拟框架的替代方法是应用函数式编程外观 here for Llewellyn Falco's peel and slice techniquehere Arlo Belshee's no mocks approach .因此,而不是像这样的方法:

public static bool Is32BitOS(IRetrieveOsAndArchitecture roa)
{
string os = roa.GetOs();
string architecture = roa.GetArchitecure();

return Is32BitOS(os, architecture);
}

你可以使用类似的东西:

public static bool Is32BitOS(Func<ManagementObjectSearcher, string> osRetriever, 
Func<ManagementObjectSearcher, string> archRetriever,
ManagementObjectSearcher searcher)
{
string os = osRetriever(searcher);
string architecture = archRetriever(searcher);

return Is32BitOS(os, architecture);
}

它的客户端方法是:

public static bool Is32BitOS()
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");

return Is32Bit((s)=>{ (from x in s.Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString().Trim()},
(s)=>{ (from x in s.Get().OfType<ManagementObject>() select x.GetPropertyValue("OSArchitecture")).First().ToString();},
searcher);
}

请注意,在测试接口(interface)和功能的情况时,您没有使用真正的 ManagementObjectSearcher 外部依赖项;在 mockist 方法中,您将在函数式编程中使用模拟对象,您将传入一个不同的 lambda 表达式,该表达式应仅返回操作系统和体系结构字符串。

这个方法还有一个责任,就是创建 ManagementObjectSearcher;要解决此依赖关系,您可以:

  1. 将其作为方法的参数传递
  2. 让它成为你类(class)的领域,在构建时初始化

关于c# - 重写 OS 查询方法以变得可测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16084803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com