I understand how to mock interfaces or virtual method calls. But frameworks like TypeMock can mock everything in the framework. Which .NET mechanisms are used to provide such functions?
The unit tests with mocks are to make sure the implementation of the class is correct. You mock the public interfaces of the dependencies of the code that you are testing.
Only use a mock (or test double) “when testing things that cross the dependency inversion boundaries of the system” (per Bob Martin). If I truly need a test double, I go to the highest level in the class hierarchy diagram above that will get the job done. In other words, don't use a mock if a spy will do.
In short, mocking is creating objects that simulate the behavior of real objects. At times you may want to distinguish between mocking as opposed to stubbing.
What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.
Typemock Isolator for example uses the CLR profiler API to intercept .NET calls and change them during runtime:
Quote:
Typemock Isolator uses an aspect-oriented programming design that creates a mock aspect. Internally, it uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, Typemock Isolator retrieves the IL and replaces it with instrumented IL code. Typemock Isolator does not change your original IL code, it simply inserts new code that calls the Typemock Isolator framework and returns mocked values.
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