Extension:MWUnit/Test doubles

Sometimes it is difficult to test certain templates because they rely on other templates or components that cannot be used in the test environment. This could be because they will not return the results needed or because executing those templates would have undesirable side effects.

When writing tests, we can replace templates and parser with test doubles, or 'mocks'. These templates and parser functions provide the same API as the original template, but their output is controlled via the test.

Mocking templates
The  parser function replaces the given template with our mock content.

 ...

It is also possible to mock pages in other namespaces:

 ...

The content of the mock is only parsed whenever the template is used and can therefore contain template specific syntax. If we want a mock to return its first argument, we can do the following:

 ...

Whenever a mocked page is transcluded in a test, the actual content of the page will be ignored and will be replaced with the mocked content. It is only possible to mock existing pages. Mocking a page that does not exist will result in an error.

Mocking parser functions
The  parser function is used to overwrite the callback for parser functions. The first parameter of this parser function takes the name of the parser function, without the hashtag. The second parameter of this parser function takes the content which should be returned when this parser function is called. This content is processed to have the same variable interface as templates:

 ...



...