简体   繁体   English

我可以伪造HttpContent.ReadAsAsync <T> ?

[英]Can I fake HttpContent.ReadAsAsync<T>?

Is it possible to create a stub that implements HttpContent.ReadAsAsync<T> with the help of Microsoft Fakes (in Visual Studio 2012)? 是否可以借助Microsoft Fakes (在Visual Studio 2012中)创建实现HttpContent.ReadAsAsync <T>的存根? If so, how? 如果是这样,怎么办?

HttpContent.ReadAsAsync<T> cannot be stubbed in VS2012. 在VS2012中,无法对HttpContent.ReadAsAsync<T>进行存根。 Reason being that this is not a virtual method but an extension method. 原因是这不是virtual方法,而是扩展方法。 Only virtual interface/class method can be stubbed. 只能存根virtual接口/类方法。

For the sake of future searches... 为了将来的搜索...

Stubs can only override methods for their own instance. 存根只能覆盖其自己实例的方法。 You can stub any type which exposes members that can be overridden, be it abstract, interface, or a class. 您可以对任何公开可以被覆盖的成员的类型进行存根,无论是抽象,接口还是类。

In this case, you could use a shim: 在这种情况下,您可以使用垫片:

ShimHttpContent.ReadAsAsyncOf1<TypeYouWant> = *something matching the method signature*

The added advantage this offers is that you can now specify the behavior per type. 此功能的另一个好处是,您现在可以指定每种类型的行为。 This is one of the most valid reasons for using shims. 这是使用垫片的最有效理由之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM