11

I am trying to make a shim in VS 2012 ultimate as it described in MSDN site:

[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);
        }
    }
}

see http://msdn.microsoft.com/en-us/library/hh549176.aspx

But when I compile my test project I get a notion in Output:

warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to 'true' and rebuild the project.

How can I resolve this warning?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
alerya
  • 3,125
  • 3
  • 28
  • 50

3 Answers3

39

Visual Studio 2012 Update 1 improved code generation in Fakes to simplify troubleshooting of code generation problems. Whenever a Stub or a Shim could not be generated for a particular type, Fakes can now generate a warning message - you can see this in the Error List window of Visual Studio.

However, to prevent the number of warnings from becoming overwhelming for a large assembly, such as System, Fakes generates a single warning by default. You can see a complete list of warning messages by setting the Diagnostic attribute of the Fakes XML element in the .Fakes file to "true" or "1" and rebuilding the project. (See the first line of code below for an example.)

To resolve the warning, change the .Fakes file to generate only those Stubs and Shims you need in your tests. Details here here a complete list of available options

<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>
JcAspes
  • 63
  • 1
  • 5
Oleg Sych
  • 6,548
  • 36
  • 34
  • 3
    Is there a way to suppress the warnings messages? I set Diagnostic to false,and Verbosity to critical but I am still getting warnings that some fakes could not be generated. – Zorayr Aug 21 '13 at 00:23
  • 1
    Yes. Use the approach described in this answer to generate only those stub and shim types that you need. Do not specify types that cannot be stubbed or shimmed. – Oleg Sych Aug 22 '13 at 17:09
  • 1
    Hi! I got some problems trying to add only specific Shims or Stubs, the Rebuild/Clean options from visual studio don't delete the precompiled Fakes Assemblies, so in case that someone got problems doing that remeber delete the folder FakesAssemblies manually. – juan25d Jun 28 '14 at 10:07
  • Is there any way to suppress even the "single warning" generated when diagnostic = false. It is not feasible to make a list of those needed or to be removed. Can't we just suppress all warnings for fakes? – Rahul Aug 08 '17 at 09:57
2

I have resolved it already alone.

It was .Net Framework 4.0 in Property.

Changing on 4.5 resolve the problem.

alerya
  • 3,125
  • 3
  • 28
  • 50
  • 4
    While changing the target .NET framework version in your unit test project can reduce the chances of encountering a type Fakes does not support, it does not eliminate this warning message directly. – Oleg Sych Dec 28 '12 at 18:19
  • con you explain how you what you mean by 'It was .Net Framework 4.0 in Property'? – Gheorghe Bulicanu Mar 25 '13 at 02:26
  • Project Property. I used .net 4.0. But 4.5 needed – alerya Mar 25 '13 at 07:21
  • @OlegSych answer should be marked as the correct one here - his response is more relevant to the question than what is currently marked as the answer. – Rockdocta Apr 01 '13 at 12:29
0

Try removing those .Fakes file. I removed them accidentally and compiled, and the warnings are gone. I am not sure what the impact is, but everything seems to run fine. I think it causes the compile to recompile the fakes file everything there is a build.

Makubex
  • 73
  • 5
  • 3
    If you remove the .fakes file, you remove the ability to do fakes. It would completely defeat the purpose. – crthompson Aug 04 '15 at 19:06