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?
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:
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 ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With