Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to mock/fake an extension method?

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)
like image 980
Omu Avatar asked Jan 30 '12 13:01

Omu


2 Answers

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.

like image 54
k.m Avatar answered Jan 22 '23 01:01

k.m


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.

like image 20
TrueWill Avatar answered Jan 22 '23 03:01

TrueWill