我一直有一个问题,痣类型不工作在静态构造函数。我已经创建了两个简单的例子来说明这个问题:痣不能在静态构造函数中工作
我有一个简单的实例类如下:
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类,但调用的类),痣不再有效:
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不再负责我的FileSystem类,所以单元测试正在调用正在文件系统上寻找文件的原始实现。
所以,唯一的变化就是Moled方法被调用的类现在是静态的。我没有更改Moled类或方法,它们仍然是实例类型,所以我不能使用Implementation.Moles.SFileSystem 语法,因为这将用于模拟静态类。
请有人可以帮助解释如何让痣在静态方法/构造函数中工作?
非常感谢!
+1写得很好的解释! – TheSilverBullet