10

I'm using a controller extension, and I tried to mock it using FakeItEasy (v 1.7.4) like this:

A.CallTo(() => controller.RenderView(A<string>.Ignored,A<object>.Ignored,null)).Returns("");

but I get this error:

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)
4

3 回答 3

15

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

你可以看看TypeMockJustMock,它们确实有这样的功能。

于 2012-01-30T14:03:40.510 回答
3

如果扩展方法在单独的程序集中声明,您可以链接到具有相同命名空间的替换程序集。

不过,您也必须从该程序集中替换任何其他必需的类型。

于 2012-01-30T14:15:46.027 回答
-1

在 FakeItEasy 中,您可以将其创建为严格的模拟,然后配置静态方法

http://hocke.blogspot.com.ar/2011/03/extension-method-for-creating-strict.html

于 2013-07-25T21:10:11.573 回答