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
    {

    };
}
David J
  • 263
  • 2
  • 10
  • Can you share your code? Or at least enough to demonstrate the issue? – Ryan Gates Apr 22 '13 at 18:40
  • I added the TcpClient shim code that I'm hoping to generate – David J Apr 22 '13 at 19:09
  • What method are you trying to shim? If it is virtual you can use [NSubstitute](http://nsubstitute.github.io/), if not you are stuck using Fakes. – Ryan Gates Apr 22 '13 at 19:12
  • 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. – David J Apr 22 '13 at 19:19
  • Without the method being virtual or an interface (which GetStream() is neither) you will not be able to use NSubstitute. – Ryan Gates Apr 22 '13 at 19:21
  • Yeah, I just looked it up. NSub looks very similar to Moq. Thanks for the suggestion though – David J Apr 22 '13 at 19:22
  • 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. – Ryan Gates Apr 22 '13 at 20:21

2 Answers2

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.

Jason Capriotti
  • 1,836
  • 2
  • 17
  • 33
David J
  • 263
  • 2
  • 10
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.

Gabriel Bourgault
  • 842
  • 11
  • 22
Fery
  • 471
  • 1
  • 5
  • 15
  • I am using VS.Net Ultimate 2012 – Fery Nov 12 '14 at 22:37
  • I changed the files like below and I have ShimBackgroundWorker now – Fery Nov 13 '14 at 15:21
  • 1
    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." [Source](https://social.msdn.microsoft.com/Forums/vstudio/en-US/0b0d14bc-9ae6-4a6a-943a-6c19a3969640/switching-from-moles-to-fakes-mwindowsidentity-to-shimwindowsidentity?forum=vsunittest) – Gabriel Bourgault Aug 03 '17 at 15:19
  • This worked for us with the System.Net.WebClient, remember to rebuild the solution! – DrLime2k10 May 16 '18 at 07:27