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)
Omu
  • 69,856
  • 92
  • 277
  • 407

3 Answers3

15

This is not possible. Proxying/intercepting libraries used by FakeItEasy (and other popular free frameworks, like Moq or RhinoMocks) don't allow interception of static methods (static properties, sealed classes and non-virtual instance methods in fact). And extension method is just a kind of static method.

You can take a look at TypeMock or JustMock, which do have such functionality.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • 1
    Alternatively, if you want a free option, Moles also allows mocking of static methods: http://research.microsoft.com/en-us/projects/pex/ – Erik Dietrich Jan 30 '12 at 19:18
  • 4
    This may be coming to FakeItEasy. How soon, I'm not sure - https://github.com/FakeItEasy/FakeItEasy/issues/90 – Adam Ralph May 21 '13 at 06:10
3

If the extension method is declared in a separate assembly, you could link in a replacement assembly with the same namespace.

You'd have to replace any other required types from this assembly too, though.

TrueWill
  • 25,132
  • 10
  • 101
  • 150
  • 1
    +1: that's really interesting solution as long as you don't have to replace too much. – k.m Jan 31 '12 at 00:08
  • @jimmy_keen Thanks! It's basically the link seam from [Working Effectively with Legacy Code](http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052). – TrueWill Jan 31 '12 at 01:18
-2

In FakeItEasy you can create it as a strict mock and then configure the static method

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

Sr.PEDRO
  • 1,664
  • 19
  • 21
  • this has absolutely nothing to do with faking extension methods. Your google-foo has let you down here – Alex Nov 20 '15 at 23:27