Help:Using mock objects

Writing tests requires diligence and careful orchestration in order for a test to be meaningful both in a way it handles input and how it asserts expected output.

In most cases where a class is covered by a test, the class should be treated as unit (hence the name "unit test") and should try to eliminate any uncontrollable influence( use of $GLOBALS etc. ) that is not directly handled by the class "under test". Achieving such separation is difficult without relying on outside (objects that under no control of the class being tested) dependencies. In order to control such dependencies the use of Fake- or MockObjects is advisable as enables part of the functionality to be simulated (create canned behaviour) that would be expected in a production environment. Another advantage of a mock object is the ability to alter conditions (being a normal Title, being a Title marked as SpecialPage etc.) so that dependent objects can be monitored on whether to produce expected results or not.

MockObjectBuilder
Semantic MediaWiki makes it a bit easier to create readable mock objects by using the while object definitions are kept in the class.

For example, if a test would need to create a mock object it would need to create the following in each test that where rely on a mocked Title.

Fortunately, Semantic MediaWiki provides a short cut for most common used mock objects within its test environment.

Callbacks
For even greater flexibility, a callback can be invoked to manipulate dependencies where more individual fine tuning is required by the test.

Of course, sometimes it is unavoidable to use the standard getMockBuilder instead because the object that needs to be mocked requires a special setup that would be beyond the scope of the.