【发布时间】:2016-08-09 09:55:24
【问题描述】:
NUnit 3.4.1,JustMock 2016.2.713.2
我有正在测试的课程:
public class AppManager {
public string[] GetAppSets() => Registry.LocalMachine
.OpenSubKey(@"SOFTWARE\Autodesk\AutoCAD", false)
?.GetSubKeyNames();
}
另外,我对GetAppSets 方法进行了测试:
[Test]
public void GetAppSets_Returns_ValidValue() {
const string subkey = @"SOFTWARE\Autodesk\AutoCAD";
/* The sets of applications which are based on
* AutoCAD 2009-2017. */
string[] fakeSets = new[] { "R17.2", "R18.0",
"R18.1", "R18.2", "R19.0", "R19.1", "R20.0",
"R20.1","R21.0" };
RegistryKey rk = Mock.Create<RegistryKey>();
Mock.Arrange(() => rk.GetSubKeyNames()).Returns(
fakeSets);
Mock.Arrange(() => Registry.LocalMachine.OpenSubKey
(subkey, false)).Returns(rk);
AppManager appMng = new AppManager();
string[] appSets = appMng.GetAppSets();
Assert.AreEqual(fakeSets, appSets);
}
它有效。但如果GetAppSets 方法使用“Software\Autodesk\AutoCAD”或“software\autodesk\autocad”字符串而不是“SOFTWARE”,我的测试将失败\Autodesk\AutoCAD":如果字符串大小写发生变化,appSets 变量将为 null(因为我的计算机上不存在该注册表项)。
因此,在这种情况下,或者测试人员需要知道GetAppSets方法的实现(坏的变体),或者来处理不区分大小写的参数 字符串。
是否可以使用第二种变体?
【问题讨论】:
标签: c# unit-testing testing justmock