asp.net-mvc – 是否可以模拟/伪造扩展方法?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 是否可以模拟/伪造扩展方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用控制器扩展,我试图使用FakeItEasy(v 1.7.4)模拟它,如下所示:
A.CallTo(() => controller.RenderView(A<string>.Ignored,A<object>.Ignored,null)).Returns("");

但我得到这个错误

System.NullReferenceException : Object reference not set to an instance of an object.
at System.Object.GetType()
at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method,Object callTarget,ref String failReason)
at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method,Object callTarget)
at FakeItEasy.Configuration.FakeConfigurationManager.CallTo(Expression`1 callSpecification)

解决方法

这不可能. FakeItEasy(以及其他流行的免费框架,如Moq或RhinoMocks)使用的代理/拦截库不允许拦截静态方法(实际上是静态属性,密封类和非虚拟实例方法).而扩展方法只是一种静态方法.

您可以查看具有此类功能TypeMockJustMock.

原文链接:https://www.f2er.com/aspnet/251646.html

猜你在找的asp.Net相关文章