- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在检查我们应用程序的单元测试并改进/添加更多单元测试。我在单元测试/测试驱动开发方面相当(不,非常)新手,我发现了我想要测试的以下方法。我被卡住了,我的问题是是否有办法重写它以使其可测试?
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 个职责:
为了启用测试,我将在 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 technique和 here 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;要解决此依赖关系,您可以:
关于c# - 重写 OS 查询方法以变得可测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16084803/
我在 android 代码中使用 asmack XMPP。我可以正常登录 XMPP 服务器,但是当我尝试创建新用户时出现问题。我想要实现的是: 以管理员身份登录。 创建一个新用户。 从管理员注销。 以
这是我的标记页面,其中有一个按钮可以从数据库中搜索数据并显示在网格中 这是我背后的代码 if (!IsPostBack) { LblInfo.Text = "Page Load
当我多次将相同的 float 值插入到我的集合中时,本应花费恒定时间的 x in s 检查变得非常慢。为什么? 时序x in s的输出: 0.06 microseconds 0.09 mi
我有一个小型聊天客户端,可以将所有历史记录存储在 sqlite 数据库中。当用户单击我的应用程序中的 history 选项卡时,我的应用程序会获取所有相关历史记录并将其显示在 QWebView 中。我
我是一名优秀的程序员,十分优秀!