Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock everything?

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?

like image 203
Primary Key Avatar asked Sep 09 '10 21:09

Primary Key


People also ask

Should you mock everything in a unit test?

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.

When should you not use a mock?

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.

What technique is mocked?

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 used for?

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.


1 Answers

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.

like image 195
Darin Dimitrov Avatar answered Oct 05 '22 04:10

Darin Dimitrov