C# 6.0,
NUnit 3.4.1 (NuGet package),
NUnit3TestAdapter 3.4.1 (NuGet package),
.Net Framework 4.6.1,
JustMock 2016.2.713.2,
Windows 10 x64,
Testable project uses .Net Framework 4.6.1 also.
We will assume that I have some class under test:
public class AppManager {
public string[] GetAppSets() => Registry.LocalMachine
.OpenSubKey(@"SOFTWARE\Autodesk\AutoCAD", false)
?.GetSubKeyNames();
}
Also, I have the test for
GetAppSets method:
[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);
}
This test works successfully.
But this test will be failure if
GetAppSets method uses "
Software\Autodesk\AutoCAD" or "
software\autodesk\autocad" string instead of "
SOFTWARE\Autodesk\AutoCAD": the
appSets variable will be
null if string case will be changed (
because that registry key doesn't exist on my computer).
So, at this case
either tester needs to know the
GetAppSets method implementation (
the bad variant),
or to handle parameter like the case insensitive string.
Is it possible to use the second variant?