Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟注册表和文件系统_C#_Wpf_Unit Testing_Filesystems_Registry - Fatal编程技术网

C# 模拟注册表和文件系统

C# 模拟注册表和文件系统,c#,wpf,unit-testing,filesystems,registry,C#,Wpf,Unit Testing,Filesystems,Registry,如果我要编写用于读取/写入/创建注册表项或文件的单元测试,我的理解是,我不应该使用真正的注册表和文件系统,而应该以轻量级的方式模拟它们 对于在C桌面/WPF风格的应用程序中模拟这些应用程序,您建议使用什么样的模拟框架? 关于这个主题,什么是好的入门读物?好的,下面是一个例子 鉴于这些类别: public interface IRegistryActions { bool WriteValue(string key, string value); } public class Regi

如果我要编写用于读取/写入/创建注册表项或文件的单元测试,我的理解是,我不应该使用真正的注册表和文件系统,而应该以轻量级的方式模拟它们

对于在C桌面/WPF风格的应用程序中模拟这些应用程序,您建议使用什么样的模拟框架? 关于这个主题,什么是好的入门读物?

好的,下面是一个例子

鉴于这些类别:

public interface IRegistryActions
{
     bool WriteValue(string key, string value);
}

public class RegistryActions : IRegistryActions
{
    public bool WriteValue(string key, string value)
    {
        // pseudocode
        // var key = Registry.OpenKey(key);
        // Registry.WriteValue(key, value);
    }
}
这个使用它们的类:在本例中,将执行操作的类被传递给构造函数,但也可以很容易地成为属性。这意味着,只要您想在实际代码中实际使用该类,就可以显式地将实现IRegistryActions的类作为参数传递-例如var exampleClass=new exampleClass new RegistryActions;-或者,如果传递null,则默认为实际实现,即this.registryActions=registryActions??新的登记行动

因此,在单元测试中,您需要验证调用是否使用了正确的参数。如何做到这一点取决于您使用的模拟框架,这通常是个人选择的问题,或者您使用已经使用的框架

[Test]
public void Test_Registry_Writes_Correct_Values()
{
     string key = "foo";
     string value = "bar";

     // you would normally do this next bit in the Setup method or test class constructor rather than in the test itself
     Mock<IRegistryActions> mock = MockFramework.CreateMock<IRegistryActions>();

    var classToTest = new ExampleClass(mock); // some frameworks make you pass mock.Object

    // Tell the mock what you expect to happen to it
    mock.Expect(m => m.WriteValue(key, value));

   // Call the action:
   classToTest.WriteValue(key, value);

   // Check the mock to make sure it happened:
   mock.VerifyAll();
}
在这里,您声明您的类已在接口上调用了正确的方法,并传递了正确的值。

好的,下面是一个示例

鉴于这些类别:

public interface IRegistryActions
{
     bool WriteValue(string key, string value);
}

public class RegistryActions : IRegistryActions
{
    public bool WriteValue(string key, string value)
    {
        // pseudocode
        // var key = Registry.OpenKey(key);
        // Registry.WriteValue(key, value);
    }
}
这个使用它们的类:在本例中,将执行操作的类被传递给构造函数,但也可以很容易地成为属性。这意味着,只要您想在实际代码中实际使用该类,就可以显式地将实现IRegistryActions的类作为参数传递-例如var exampleClass=new exampleClass new RegistryActions;-或者,如果传递null,则默认为实际实现,即this.registryActions=registryActions??新的登记行动

因此,在单元测试中,您需要验证调用是否使用了正确的参数。如何做到这一点取决于您使用的模拟框架,这通常是个人选择的问题,或者您使用已经使用的框架

[Test]
public void Test_Registry_Writes_Correct_Values()
{
     string key = "foo";
     string value = "bar";

     // you would normally do this next bit in the Setup method or test class constructor rather than in the test itself
     Mock<IRegistryActions> mock = MockFramework.CreateMock<IRegistryActions>();

    var classToTest = new ExampleClass(mock); // some frameworks make you pass mock.Object

    // Tell the mock what you expect to happen to it
    mock.Expect(m => m.WriteValue(key, value));

   // Call the action:
   classToTest.WriteValue(key, value);

   // Check the mock to make sure it happened:
   mock.VerifyAll();
}

在这种情况下,您断言您的类调用了接口上的正确方法,并传递了正确的值。

模拟注册表和文件等静态类不容易,也不可能?除非您购买例如TypeMock。如果您可以更改正在测试的代码,请考虑在实现适当接口的类中包装实际注册表和文件访问- IRegistryXXX,IFileXXX-这样您就可以模拟接口,单元测试可以检查您是否在正确的时间进行了适当的调用。@stuardt是否可以举一个简单的例子。我对这一点还不熟悉,不确定我是否了解。非常感谢。没问题,请参阅答案模拟注册表和文件等静态类不容易或不可能?除非您购买例如TypeMock。如果您可以更改正在测试的代码,请考虑在实现适当接口的类中包装实际注册表和文件访问- IRegistryXXX,IFileXXX-这样您就可以模拟接口,单元测试可以检查您是否在正确的时间进行了适当的调用。@stuardt是否可以举一个简单的例子。我对这一点还不熟悉,不确定我是否了解。非常感谢。没问题,请参阅答案,以澄清问题,谢谢。你会推荐哪种模拟框架?我个人无法推荐——我喜欢的框架实际上已经不再维护了,我也不喜欢其他人似乎都喜欢的框架。感谢你的帮助和回答StuardtI做了更多的调查,并想出了将你上面建议的与Rhinomock相结合的方法。非常感谢~这是我在自己的项目中一直使用的一个:它澄清了这一点,谢谢。你会推荐哪种模拟框架?我个人无法推荐——我喜欢的框架实际上已经不再维护了,我也不喜欢其他人似乎都喜欢的框架。感谢你的帮助和回答StuardtI做了更多的调查,并想出了将你上面建议的与Rhinomock相结合的方法。非常感谢~这是我在自己的项目中一直使用的一个: