vs 2012:Shims编译

ale*_*rya 10 shim microsoft-fakes visual-studio-2012

我试图在VS 2012终极中制作一个垫片,如MSDN网站中所述:

[TestClass]
public class TestClass1
{ 
    [TestMethod]
    public void TestCurrentYear()
    {
        int fixedYear = 2000;

        using (ShimsContext.Create())
        {
          // Arrange:
          // Detour DateTime.Now to return a fixed date:
          System.Fakes.ShimDateTime.NowGet = 
              () =>
              { return new DateTime(fixedYear, 1, 1); };

          // Instantiate the component under test:
          var componentUnderTest = new MyComponent();

          // Act:
          int year = componentUnderTest.GetTheCurrentYear();

          // Assert: 
          // This will always be true if the component is working:
          Assert.AreEqual(fixedYear, year);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅http://msdn.microsoft.com/en-us/library/hh549176.aspx

但是当我编译我的测试项目时,我在输出中得到了一个概念:

警告:无法生成一些假货.有关完整的详细信息,请将此文件中Fakes元素的Diagnostic属性设置为"true"并重建项目.

我该如何解决此警告?

Ole*_*ych 39

Visual Studio 2012 Update 1改进了Fakes中的代码生成,以简化代码生成问题的故障排除.每当无法为特定类型生成Stub或Shim时,Fakes现在可以生成警告消息 - 您可以在Visual Studio的"错误列表"窗口中看到此消息.

但是,为了防止大型程序集(例如System)的警告数量过多,Fakes会默认生成一个警告.通过将.Fakes文件Diagnostic中的FakesXML元素的属性设置为"true"或"1"并重建项目,可以看到完整的警告消息列表.(有关示例,请参阅下面的第一行代码.)

要解决警告,请更改.Fakes文件以仅生成测试中所需的存根和填充程序.细节在这里 这里可供选择的完整列表

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="System" Version="4.0.0.0"/>
  <StubGeneration Disable="true" />
  <ShimGeneration>
    <Clear/>
    <Add FullName="System.DateTime!"/>
  </ShimGeneration>
</Fakes>
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法抑制警告信息?我将Diagnostic设置为false,将Verbosity设置为严重,但我仍然收到警告,表示无法生成一些假货. (3认同)