Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
Static 摩尔在静态构造函数中不起作用_Static_Constructor_Moles - Fatal编程技术网

Static 摩尔在静态构造函数中不起作用

Static 摩尔在静态构造函数中不起作用,static,constructor,moles,Static,Constructor,Moles,我一直有一个问题,鼹鼠类型不在静态构造函数中工作。我创建了两个简单的示例来解释这个问题: 我有一个简单的实例类,如下所示: public class InstanceTestReader { public InstanceTestReader () { IFileSystem fileSystem = new FileSystem(); this.Content = fileSystem.ReadAllText("test.txt");

我一直有一个问题,鼹鼠类型不在静态构造函数中工作。我创建了两个简单的示例来解释这个问题:

我有一个简单的实例类,如下所示:

public class InstanceTestReader
{
    public InstanceTestReader ()
    {
        IFileSystem fileSystem = new FileSystem();

        this.Content = fileSystem.ReadAllText("test.txt");
    }

    public string Content { get; private set; }
}
    [TestMethod]
    [HostType("Moles")]
    public void CheckValidFileInstance_WithMoles()
    {
        // Arrange
        string expectedFileName = "test.txt";
        string content = "test text content";

        Implementation.Moles.MFileSystem.AllInstances.ReadAllTextString = (moledTarget, suppliedFilename) =>
        {
            Assert.AreEqual(suppliedFilename, expectedFileName, "The filename was incorrect");
            return content;
        };

        // Act
        string result = new InstanceTestReader().Content;

        // Assert
        Assert.AreEqual(content, result, "The result was incorrect");
    }
我对此有一个单元测试,如下所示:

public class InstanceTestReader
{
    public InstanceTestReader ()
    {
        IFileSystem fileSystem = new FileSystem();

        this.Content = fileSystem.ReadAllText("test.txt");
    }

    public string Content { get; private set; }
}
    [TestMethod]
    [HostType("Moles")]
    public void CheckValidFileInstance_WithMoles()
    {
        // Arrange
        string expectedFileName = "test.txt";
        string content = "test text content";

        Implementation.Moles.MFileSystem.AllInstances.ReadAllTextString = (moledTarget, suppliedFilename) =>
        {
            Assert.AreEqual(suppliedFilename, expectedFileName, "The filename was incorrect");
            return content;
        };

        // Act
        string result = new InstanceTestReader().Content;

        // Assert
        Assert.AreEqual(content, result, "The result was incorrect");
    }
这是没有问题的

但是,如果我将调用类更改为静态(不是Moled类,而是调用类),Moles将不再工作:

public static class StaticTestReader
{
    static StaticTestReader ()
    {
        IFileSystem fileSystem = new FileSystem();

        Content = fileSystem.ReadAllText("test.txt");
    }

    public static string Content { get; private set; }
}
并相应地修改我的单元测试:

    [TestMethod]
    [HostType("Moles")]
    public void CheckValidFileStatic_WithMoles()
    {
        // Arrange
        string expectedFileName = "test.txt";
        string content = "test text content";

        Implementation.Moles.MFileSystem.AllInstances.ReadAllTextString = (moledTarget, suppliedFilename) =>
        {
            Assert.AreEqual(suppliedFilename, expectedFileName, "The filename was incorrect");
            return content;
        };

        // Act
        string result = StaticTestReader.Content;

        // Assert
        Assert.AreEqual(content, result, "The result was incorrect");
    }
。。。现在鼹鼠不再工作了。运行此测试时,出现错误“找不到文件‘d:\blah\blah\test.txt’”。我之所以会这样,是因为Moles不再负责我的文件系统类,所以单元测试正在调用原始实现,该实现正在文件系统上查找文件

所以,唯一的变化是调用Moled方法的类现在是静态的。我没有更改Moled类或方法,它们仍然是实例类型,因此我无法使用Implementation.Moles.SFileSystem 这将是用于模拟静态类的语法

请有人帮我解释一下如何让Moles在静态方法/构造函数中工作


非常感谢

静态构造函数不同于静态方法。通过一个方法,您可以控制调用它的时间和地点。在执行对类的任何访问之前,运行库会自动调用构造函数,在这种情况下,会导致在设置
文件系统的mole之前调用构造函数,从而导致您看到的错误

如果您将实现更改为以下内容,那么它应该可以工作

public static class StaticTestReader
{
    private static string content;

    public static string Content
    {
        get
        {
            if (content == null)
            {
                IFileSystem fileSystem = new FileSystem();

                content = fileSystem.ReadAllText("test.txt");
            }

            return content;
        }
    }
}

更新:

但是,如果您无法更改实现,Moles提供的唯一其他替代方法是阻止执行静态构造函数中的代码,然后直接mole
内容
属性。要删除类型的静态构造函数,您需要在测试程序集中包含以下程序集级别属性:

[assembly: MolesEraseStaticConstructor(typeof(StaticTestReader))]

静态构造函数不同于静态方法。通过一个方法,您可以控制调用它的时间和地点。在执行对类的任何访问之前,运行库会自动调用构造函数,在这种情况下,会导致在设置
文件系统的mole之前调用构造函数,从而导致您看到的错误

如果您将实现更改为以下内容,那么它应该可以工作

public static class StaticTestReader
{
    private static string content;

    public static string Content
    {
        get
        {
            if (content == null)
            {
                IFileSystem fileSystem = new FileSystem();

                content = fileSystem.ReadAllText("test.txt");
            }

            return content;
        }
    }
}

更新:

但是,如果您无法更改实现,Moles提供的唯一其他替代方法是阻止执行静态构造函数中的代码,然后直接mole
内容
属性。要删除类型的静态构造函数,您需要在测试程序集中包含以下程序集级别属性:

[assembly: MolesEraseStaticConstructor(typeof(StaticTestReader))]

我认为您必须从您的声明中删除所有说明。要访问静态文件,不需要类的实例

试试这个:

Implementation.Moles.MFileSystem.ReadAllTextString = (...)

我认为您必须从您的声明中删除所有说明。要访问静态文件,不需要类的实例

试试这个:

Implementation.Moles.MFileSystem.ReadAllTextString = (...)