.NET方法不会生成垫片 [英] Shims are not generated for .NET methods

查看:165
本文介绍了.NET方法不会生成垫片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始使用Microsoft Fakes时,我很高兴开始填充某些.NET方法.我被认为可以使用任何.NET方法(静态或非静态)都可以:

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.

但是,我一直试图对TcpClient中的某些方法进行填充,并且仅创建存根,这对我没有好处,因为我希望能够更改某些方法以返回我自己的数据,而不是依赖于在实时的TcpClient上给我数据.

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.

如果Microsoft Fakes之外还有其他方法,我愿意就如何执行此操作提出任何建议.

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

添加代码以演示问题

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

    };

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

    };
}

推荐答案

解决方案是在System.fakes文件中显式添加我要填充的类.这就是我现在的样子:

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>

Remove Obsolete="1"可以防止Shim生成代码在尝试使[过时]代码趋于匀化时引发错误.

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

这篇关于.NET方法不会生成垫片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆