Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can most of the mock frameworks in .NET (Core) not mock static and private methods?

My question is about the technical reason for this limitation, not about how to fix it.

Why do some frameworks like Telerik JustMock and Typemock Isolator support these features, but we can not have these in Moq or FakeItEasy or NSubstitute, etc.?

Are the items mentioned unnecessary in unit testing?

like image 382
HamedFathi Avatar asked Jan 27 '23 06:01

HamedFathi


1 Answers

This is because of the way these libraries work. When you mock a class using Moq, NSubstitute or FakeItEasy, they dynamically create a class that inherits from that class and overrides its methods. But they have to follow the platform's rules for overriding methods:

  • non-virtual methods can't be overridden, by definition
  • private methods are not accessible to derived classes, so they can't be overriden (and in fact, C# doesn't even allow them to be virtual, as it wouldn't make sense)
  • static methods can't be overridden either, because it wouldn't make sense: polymorphism is based on the type of the instance on which you call the method, and static methods don't have an instance...

In fact, these mocking libraries do nothing that you couldn't have done yourself by manually writing fake/mock classes; they just make it easier by relieving you of the boilerplate code. You couldn't manually override a static or non-virtual method, and these libraries can't do it either, for the same reason.

I don't know how JustMock and TypeMock Isolator work; I suspect they do some dark magic with the CLR's internals, or maybe dynamically rewrite code (that's what Pose does: it replaces calls to the specified methods with calls to replacement methods).

EDIT: See this question about how TypeMock Isolator works. It uses the profiler API to hijack method calls. As I said, dark magic ^^

like image 157
Thomas Levesque Avatar answered Jan 28 '23 19:01

Thomas Levesque