c# - 重写操作系统查询方法以使其可测试

标签 c# unit-testing testability

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

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 外部依赖项;在模拟方法中,您将在函数式编程中使用模拟对象,您将传入一个不同的 lambda 表达式,该表达式应该仅返回操作系统和体系结构字符串。

这个方法仍然有一个责任,那就是创建 ManagementObjectSearcher;要解决这种依赖性,您可以:

  1. 将其作为方法的参数传递
  2. 使其成为您的类中在构造时初始化的字段

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

相关文章:

c# - .NET 如何对特殊字符进行排序?

inheritance - 继承与可测试性的组成

c# - 如何在 ControllerContext 中模拟 DisplayMode 以进行单元测试 c#

java - 如何将相同的单元测试应用于不同的功能

c++ - 创建可测试代码

python - 您将如何测试这个功能?

c# - 交易升级为 DTC 无多重连接

c# - 使用 P/Invoke 导致系统 AccessViolationException

c# - Window.Print() 阻止其他 google chrome 标签

c# - 为什么以下测试使用 Mbunit 和 Rhino 模拟失败?