Shims are not generated for .NET methods

本秂侑毒 提交于 2019-12-04 17:47:32

问题


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
    {

    };
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16154182/shims-are-not-generated-for-net-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!