模拟注册表-SystemWrapper

本文关键字:-SystemWrapper 注册表 模拟 | 更新日期: 2023-09-27 18:24:50

我已经尝试过以下内容作为模拟注册表的指南:http://www.rhyous.com/2011/11/04/unit-testing-registry-access-with-rhinomocks-and-systemwrapper/

当我尝试Mock它时,当它尝试调用OpenSubKey时,我的类中的"reg"总是返回null,在我的_Real()测试中,它运行良好。

测试:

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);
}

实际等级:

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
     .....
    }
}

模拟注册表-SystemWrapper

我发现了,我将处理OpenSubKey的Stub添加到初始keyMock中,而不是OpenRemoteBaseKey() 返回的那个

这是固定代码:

[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));
}