In the next several subsequent posts I want to revise mocking frameworks available today, describe key features, provide examples and compare with alternatives.
Mock-objects are usually used in unit-tests to separate the object under test from its dependencies. Unit-tests are supposed to test objects independently, so the behavior of test should depend only on code of target object. To reach that goal the object dependencies should be replaced to stubs, that are doing nothing or just enough for test to pass. The mocking framework is the thing that simplifies generation of such stubs.
Also, mock-objects could come in handy in other test types, when there is need to check interaction with modules that are not written yet, or with system that works too slow to be used in automated testing.
There are different types of mock-objects:
- stub - dumb objects.They don't contain any logic, their methods and properties return default values. Such objects are used for the cases when exact values are not needed or that dependency is not required for the test but should be passed into constructor of class under test and so on.
- mock - clever stubs. They could check whether the certain method have been called and what arguments were used. Those objects are used to check if the target object interacts with its dependencies correctly.
- fake - that is, usually, object, that has some functionality - just enough for testing.