Mocking the Registry - SystemWrapper

2019-07-28 10:45发布

问题:

I've tried following this as a guide for Mocking the registry: http://www.rhyous.com/2011/11/04/unit-testing-registry-access-with-rhinomocks-and-systemwrapper/

When I try to Mock it, I always get a null return for "reg" in my class when it tries to do the OpenSubKey call, in my _Real() test it works fine.

Tests:

private RegistryService CreateMockedRegistryService()
{
    var registryService = new RegistryService(MockRepository.GenerateMock<ILoggerFacadeExtended>(), MockRepository.GenerateMock<IConnectivityService>());
    // Mock the Base Key so we can throw errors and manipulate it
    registryService.ChangeBaseKey(MockRepository.GenerateMock<IRegistryKey>());
    return registryService;
}

private IRegistryKey CreateMockedHKLM()
{
    IRegistryKey hklmMock = MockRepository.GenerateMock<IRegistryKey>();
    hklmMock.Stub(x => x.Name).Return("HKEY_LOCAL_MACHINE");
    return hklmMock;
}

[TestMethod()]
public void GetRegistryKeyTest_Mocked()
{
    IRegistryService target = CreateMockedRegistryService();
    string machineName = "localhost";
    RegistryHive hive = RegistryHive.LocalMachine;
    string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
    string valueName = "SystemRoot";
    object defaultValue = @"C:\Windows";

    IRegistryKey keyMock = CreateMockedHKLM();
    keyMock.Stub(x => x.OpenRemoteBaseKey(hive, machineName)).Return(CreateMockedHKLM());
    var subKeyMock = MockRepository.GenerateMock<IRegistryKey>();
    subKeyMock.Stub(x => x.Name).Return(keyMock.Name + @"\" + path);
    subKeyMock.Stub(x => x.GetValueNames()).Return(new string[] { valueName });
    subKeyMock.Stub(x => x.GetValue(valueName)).Return(defaultValue);

    keyMock.Stub(x => x.OpenSubKey(path)).Return(subKeyMock);
    keyMock.Stub(x => x.GetValue(valueName)).Return(defaultValue);
    target.ChangeBaseKey(keyMock);

    IResult<object> expected = new Result<object>() { Results = defaultValue };
    IResult<object> actual;
    actual = target.GetRegistryKey(machineName, hive, path, valueName, defaultValue);

    Assert.AreEqual(expected.Results, actual.Results);
}

[TestMethod()]
public void GetRegistryKeyTest_Real()
{
    IRegistryService target = CreateMockedRegistryService();
    string machineName = "localhost";
    RegistryHive hive = RegistryHive.LocalMachine;
    string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
    string valueName = "SystemRoot";
    object defaultValue = @"C:\Windows";

    // We don't want to use the regular Mocked version but actually hit the registry so it works
    IRegistryKey baseKey = new RegistryWrap().LocalMachine;
    target.ChangeBaseKey(baseKey);

    IResult<object> expected = new Result<object>() { Results = defaultValue };
    IResult<object> actual;
    actual = target.GetRegistryKey(machineName, hive, path, valueName, defaultValue);

    Assert.AreEqual(expected.Results, actual.Results);
}

Actual Class:

private IRegistryKey _BaseKey;
public IRegistryKey BaseKey
{
    get
    {
        if (_BaseKey == null)
        {
            _BaseKey = new RegistryWrap().LocalMachine;
        }
        return _BaseKey;
    }
}

public void ChangeBaseKey(IRegistryKey inBaseKey)
{
    _BaseKey = inBaseKey;
}

public IResult<object> GetRegistryKey(string machineName, RegistryHive hive, string path, string valueName, object defaultValue)
    {
        return GetRegistryKey(BaseKey.OpenRemoteBaseKey(hive, machineName), path, valueName, defaultValue);
    }
}

public IResult<object> GetRegistryKey(IRegistryKey baseKey, string path, string valueName, object defaultValue)
{
    IResult<object> result = new Result<object>();
    try
    {
        if (baseKey == null) throw new ArgumentNullException("baseKey");
        if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("path");
        if (string.IsNullOrWhiteSpace(valueName)) throw new ArgumentException("valueName");
        var reg = baseKey.OpenSubKey(path);
        result.Results = reg.GetValue(valueName); // reg is null when I try to Mock it
     .....
    }
}

回答1:

I figured it out, I was adding the Stubs dealing with OpenSubKey to the initial keyMock and not the one that was returned by OpenRemoteBaseKey()

Here is the fixed code:

[TestMethod()]
public void GetRegistryKeyTest_Mocked()
{
    IRegistryService target = CreateMockedRegistryService();
    string machineName = "localhost";
    RegistryHive hive = RegistryHive.LocalMachine;
    string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
    string valueName = "SystemRoot";
    object defaultValue = @"C:\Windows";


    var keyMock = CreateMockedHKLM();
    var remoteKeyMock = CreateMockedHKLM();
    var subKeyMock = MockRepository.GenerateMock<IRegistryKey>();
    subKeyMock.Stub(x => x.Name).Return(keyMock.Name + @"\" + path);
    subKeyMock.Stub(x => x.GetValueNames()).Return(new string[] { valueName });
    subKeyMock.Stub(x => x.GetValue(valueName)).Return(defaultValue);
    remoteKeyMock.Stub(x => x.OpenSubKey(path)).Return(subKeyMock);
    remoteKeyMock.Stub(x => x.GetValue(valueName)).Return(defaultValue);

    keyMock.Stub(x => x.OpenRemoteBaseKey(hive, machineName)).Return(remoteKeyMock);
    target.ChangeBaseKey(keyMock);

    actual = target.GetRegistryKey(machineName, hive, path, valueName, defaultValue);

    Assert.AreEqual(expected.Results, actual.Results);
    subKeyMock.AssertWasCalled(x => x.GetValue(valueName));
}