0

The following method in my class library works fine when I call them from ConsoleApp. But when I try to unit test it, the registry value is not updated. Why?

I get a null-reference error.

namespace ClassLibrary1
{
    public class Class1
    {
        public static void MyMethod()
        {
            string targketKey = @"SOFTWARE\MyApp1";

            using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(targetKey, true))
            {
                rk.SetValue("target", "new value", Microsoft.Win32.RegistryValueKind.String);
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Perhaps a difference with credentials between launch of the Console application and Visual Studio? – HuorSwords May 22 '14 at 18:01
  • Can you help me understand why you're comparing Visual Studio and Console. You create the Console Application in Visual Studio. I apologize for my ignorance. – Rod May 22 '14 at 18:07
  • I don't compare both applications, only the credentials with you run each of them. If you are developing on Windows 7 or Windows 8.x, try to execute Visual Studio as Administrator and launch your unit test. – HuorSwords May 22 '14 at 18:14
  • Administrator is what my Visual Studio was running as (right-clicked on Visual Studio and clicked Run as administrator) – Rod May 22 '14 at 18:17
  • Is your testing project targeting the same CPU architecture as your class library? The registry has different branches for 32-bit and 64-bit entries, which can cause problems like this. For example, see this answer: http://stackoverflow.com/a/2464420/926713 – Lilshieste May 22 '14 at 19:38

1 Answers1

1

After some tests over your code, I think that I found what is your problem. Please, try this:

  • Ensure that the HKLM\SOFTWARE\MyApp1 key is created.
  • Change the target platform of your class project from Any CPU to x64.
  • Keep the target platform of your test project to Any CPU.
  • Set the Test's default architecture to x64 in the menu

Test > Test Settings > Default Processor Architecture > x64

  • Check this code.

public static void MyMethod()
{
    var rootKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
    using (var existingKey = rootKey.OpenSubKey("MyApp1", true))
    {
        existingKey.SetValue("target", "double new");
        existingKey.Close();
    }    

    rootKey.Close();
}

I don't understand why, but if you try to initialize existingKey directly (without rootKey), then it value is null.

HuorSwords
  • 2,225
  • 1
  • 21
  • 34
  • When I change my target platform to x64 the test disappears in test explorer. – Rod May 22 '14 at 20:19
  • @Rod, you are right. Please review my answer: I updated it to show you how avoid this problem. – HuorSwords May 22 '14 at 20:31
  • Thanks! that worked. Btw, Is there a way to run this code without having to Run VS as administrator? I started looking into Code Access Security but haven't had any luck. – Rod May 22 '14 at 20:41
  • I think that the better solution will be that when you create your main registry key, you give `Full Control` access to `Users` group. On [MSDN](http://support.microsoft.com/kb/237607/en-us) there is a KB that explains how to use regini to set permissions on registry keys. – HuorSwords May 22 '14 at 20:42