title: Mocking the Registry - SystemWrapper
body: <p>I've tried following this as a guide for Mocking the registry: <a href="http://www.rhyous.com/2011/11/04/unit-testing-registry-access-with-rhinomocks-and-systemwrapper/" rel="nofollow">http://www.rhyous.com/2011/11/04/unit-testing-registry-access-with-rhinomocks-and-systemwrapper/</a></p>
  
  <p>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.</p>
  
  <p>Tests:</p>
  
  <pre><code>private RegistryService CreateMockedRegistryService()
  {
      var registryService = new RegistryService(MockRepository.GenerateMock&lt;ILoggerFacadeExtended&gt;(), MockRepository.GenerateMock&lt;IConnectivityService&gt;());
      // Mock the Base Key so we can throw errors and manipulate it
      registryService.ChangeBaseKey(MockRepository.GenerateMock&lt;IRegistryKey&gt;());
      return registryService;
  }
  
  private IRegistryKey CreateMockedHKLM()
  {
      IRegistryKey hklmMock = MockRepository.GenerateMock&lt;IRegistryKey&gt;();
      hklmMock.Stub(x =&gt; 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 =&gt; x.OpenRemoteBaseKey(hive, machineName)).Return(CreateMockedHKLM());
      var subKeyMock = MockRepository.GenerateMock&lt;IRegistryKey&gt;();
      subKeyMock.Stub(x =&gt; x.Name).Return(keyMock.Name + @"\" + path);
      subKeyMock.Stub(x =&gt; x.GetValueNames()).Return(new string[] { valueName });
      subKeyMock.Stub(x =&gt; x.GetValue(valueName)).Return(defaultValue);
  
      keyMock.Stub(x =&gt; x.OpenSubKey(path)).Return(subKeyMock);
      keyMock.Stub(x =&gt; x.GetValue(valueName)).Return(defaultValue);
      target.ChangeBaseKey(keyMock);
  
      IResult&lt;object&gt; expected = new Result&lt;object&gt;() { Results = defaultValue };
      IResult&lt;object&gt; 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&lt;object&gt; expected = new Result&lt;object&gt;() { Results = defaultValue };
      IResult&lt;object&gt; actual;
      actual = target.GetRegistryKey(machineName, hive, path, valueName, defaultValue);
  
      Assert.AreEqual(expected.Results, actual.Results);
  }
  </code></pre>
  
  <p>Actual Class:</p>
  
  <pre><code>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&lt;object&gt; GetRegistryKey(string machineName, RegistryHive hive, string path, string valueName, object defaultValue)
      {
          return GetRegistryKey(BaseKey.OpenRemoteBaseKey(hive, machineName), path, valueName, defaultValue);
      }
  }
  
  public IResult&lt;object&gt; GetRegistryKey(IRegistryKey baseKey, string path, string valueName, object defaultValue)
  {
      IResult&lt;object&gt; result = new Result&lt;object&gt;();
      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
       .....
      }
  }
  </code></pre>