Shims are not generated for .NET methods
Asked Answered
A

2

16

When I began using Microsoft Fakes, I was excited to start shimming some .NET methods. I was lead to believe that I would be able to shim ANY .NET method, static or not: http://msdn.microsoft.com/en-us/library/hh549176.aspx.

However, I've been trying to shim some of the methods in TcpClient and only stubs are created, which does me no good, since I want to be able to change some of the methods to return my own data rather than depending on a live TcpClient to give me data.

I'm open to any suggestions on how to do this if there is another way beyond Microsoft Fakes.

EDIT: Adding code to demonstrate the problem

[TestMethod]
public void CommunicationTest()
{
    var stubbedTcpClient = new System.Net.Sockets.Fakes.StubTcpClient
    {

    };

    //No such ShimTcpClient exists
    var shimmedTcpClient = new System.Net.Sockets.Fakes.ShimTcpClient
    {

    };
}
Alrich answered 22/4, 2013 at 18:31 Comment(7)
Can you share your code? Or at least enough to demonstrate the issue?Tendance
I added the TcpClient shim code that I'm hoping to generateAlrich
What method are you trying to shim? If it is virtual you can use NSubstitute, if not you are stuck using Fakes.Tendance
The Available and Connected properties as well as GetStream. I'll give NSubstitute a chance and see what happens. I'd still like to get a solid reason for why I can't seem to get any .NET classes to generate shim versions other than DateTime.Alrich
Without the method being virtual or an interface (which GetStream() is neither) you will not be able to use NSubstitute.Tendance
Yeah, I just looked it up. NSub looks very similar to Moq. Thanks for the suggestion thoughAlrich
If you have a solution that works, please post it as an answer and accept it. This will prevent your question from showing up as unanswered.Tendance
A
10

Got it working with help from this blog post and here.

The solution was to add the classes I wanted to shim explicitly in the System.fakes file. This is what mine looks like now:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="System" Version="4.0.0.0"/>
  <ShimGeneration>
    <Clear/>
    <Add FullName="System.Net.Sockets.TcpClient"/>
    <Remove Obsolete="1"/>
  </ShimGeneration>
</Fakes>

The Remove Obsolete="1" is to stop errors from being thrown by the Shim generation code when it attempts to shim [Obsolete] code.

Alrich answered 22/4, 2013 at 20:50 Comment(0)
I
1

I also had the same problem.

My System.fakes and mscorlib.fakes looked like this :

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="mscorlib" Version="4.0.0.0"/>
  <ShimGeneration>
    <Add Namespace="System.ComponentModel.BackgroundWorker"/>
  </ShimGeneration>
</Fakes>

and

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="System" Version="4.0.0.0"/>
  <ShimGeneration>
    <Add Namespace="System.ComponentModel.BackgroundWorker"/>
  </ShimGeneration>
</Fakes>

Solution

System.fakes

 <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
   <Assembly Name="System" Version="4.0.0.0"/>
   <ShimGeneration>
     <Add FullName="System.ComponentModel.BackgroundWorker!"/>
   </ShimGeneration>
 </Fakes>

mscorlib.fakes

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="mscorlib" Version="4.0.0.0"/>
  <ShimGeneration>
    <Add FullName="System.ComponentModel.BackgroundWorker!"/>
  </ShimGeneration>
</Fakes>

and after saving the files I Rebuild the solution. And now I have ShimBackgroundWorker.

Interceptor answered 12/11, 2014 at 22:32 Comment(4)
I am using VS.Net Ultimate 2012Interceptor
I changed the files like below and I have ShimBackgroundWorker nowInterceptor
Important note : "The exclamation mark at the end of FullName is optional. Without it, the Fakes code generator will consider all types that start with the specified string. The exclamation mark tells the code generator to look for an exact match." SourceAudreaaudres
This worked for us with the System.Net.WebClient, remember to rebuild the solution!Turbo

© 2022 - 2024 — McMap. All rights reserved.