2015-04-17 24 views
0

我试图模拟Win32注册表对象,以便隔离部分单元测试的代码。我能够创建垫片,他们被调用来执行我的代码就好。但是我不知道如何使CreateSubKey方法工作。它需要返回一个指向新创建的密钥的新RegistryKey对象。但我无法弄清楚如何做到这一点。这里是我有:如何使用假货和垫片模拟CreateRegistryKey

using (ShimsContext.Create()) 
{ 
    Microsoft.Win32.Fakes.ShimRegistryKey.AllInstances.CreateSubKeyStringRegistryKeyPermissionCheck = (key, newSubkey, permissionCheck) => 
    { 
     return key; // This returns the base, but I need a new one that adds the subkey... 
    }; 
} 

我在这里错过什么?

回答

0

好吧,我觉得以前没有想过这件事很愚蠢。我张贴后,它打我。我只需要制作一个新的ShimRegistryKey。

using (ShimsContext.Create()) 
{ 
    Microsoft.Win32.Fakes.ShimRegistryKey.AllInstances.CreateSubKeyStringRegistryKeyPermissionCheck = (key, newSubKeyName, permissionCheck) => 
    { 
     var newSubKey = new Microsoft.Win32.Fakes.ShimRegistryKey(); 
     newSubKey.NameGet =() => Path.Combine(key.Name, newSubKeyName); // All I care about for now is the name of the new subkey 
     return newSubKey; 
    }; 
} 

有时你只需提问即可看到答案。 :-)

+0

嗨,我想用Microsoft.Fakes来伪造注册表访问。奇怪的是,我没有看到Microsoft.Win32.Fakes下的注册表相关(或任何其他Shims)。我只看到一些与注册表无关的存根。我正在使用.Net 4.0系统组装和VS2017 Enterprise。有谁知道什么是错的? –