【问题标题】:The `AllInstances` using in the `MS Fakes``MS Fakes` 中使用的`AllInstances`
【发布时间】:2015-12-31 21:24:04
【问题描述】:
MS Visual Studio 2015 企业版。
.Net 框架 4.6
来自MSDN:
要为实例方法使用 shim,请在
类型名称和方法名称:
System.IO.Fakes.ShimFile.AllInstances.ReadToEnd = ...
但是当我在ShimFile 之后写. 时,IntelliSense 在 ComboBox 中没有 AllInstances 项:
我也尝试对DateTime 做同样的事情,但我发现了另一个问题:AllInstances 存在,但 IntelliSense 没有相应的项目:
为什么会出现这些错误?
【问题讨论】:
标签:
c#
.net
unit-testing
microsoft-fakes
【解决方案1】:
对于第 1 个问题,我认为您正在尝试使用错误的 Shim 类。您需要 StreamReader(其中包含 ReadToEnd 方法)。这对我有用:
string testString = "Hello World!";
System.IO.Fakes.ShimStreamReader.AllInstances.ReadToEnd = reader => testString;
对于问题 2,我不相信您可以填充结构。 DateTime 实际上是一个结构而不是一个类。这是我尝试过的一些示例代码:
public struct TestShimOfStruct
{
public int Year { get; set; }
}
public class TestShimOfClass
{
public int Year { get; set; }
}
...
[TestMethod]
public void TestStructAndClass()
{
using (ShimsContext.Create())
{
PartialTest.Fakes.ShimTestShimOfClass.AllInstances.YearGet = shimClass => 2015;
PartialTest.Fakes.ShimTestShimOfStruct.AllInstances.YearGet = shimClass => 2015;
}
}
TestMethod 在 VisualStudio 中显示此错误。
如您所见,结构失败而类成功,即使存在逻辑等价。