Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shims are not generated for .NET methods

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
    {

    };
}
like image 321
David J Avatar asked Apr 22 '13 18:04

David J


People also ask

What is shims in c#?

A shim modifies the compiled code of your application at runtime so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you can't modify, such as . NET assemblies.

What is Microsoft shims?

In computer programming, a shim is a small library which transparently intercepts an API, changes the parameters passed, handles the operation itself, or redirects the operation elsewhere. Shims can also be used for running programs on different software platforms than they were developed for.


1 Answers

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.

like image 82
David J Avatar answered Oct 05 '22 23:10

David J