Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...