Utilities for locally overriding function behavior, useful in software testing and much more
Contributed by: Jesse Friedman
The Mockingbird package provides utilities for storing definitions in an unevaluated form and installing them temporarily in a localized manner. This is particularly useful for writing software tests involving “mocked” or “stubbed” versions of built-in or custom functions, but can also be used more generally in a variety of development and debugging scenarios.
Installation Instructions
To install this paclet in your Wolfram Language environment,
evaluate this code:
PacletInstall["Wolfram/Mockingbird"]
To load the code after installation, evaluate this code:
Needs["Wolfram`Mockingbird`"]
Details
Adopting a "mocking" pattern lets you write unit tests that test a single part of a software system in isolation, with out-of-scope functions or components replaced by minimal dummy versions.
Mocking can be used to simulate the behavior of external APIs, services, or I/O operations without the expense of interacting with the real external system.
The local definition override functionality provided by Mockingbird can also be useful for interactive debugging situations where it's desirable to inspect or augment the behavior of an existing function.
Examples
Basic Examples (1)
Evaluate an expression using a mocked version of the function f:
Scope (2)
Use MockDefinitionGroup to specify multiple, potentially interconnected definitions:
By default, MockDefinition shields against recursive invocation, so a mock definition for a function can call the same function's original definition without danger of infinite recursion:
Applications (3)
Create a mocked version of URLExecute for testing the UKPostcodeLookup resource function. The mock definition validates the URL in an HTTP request and returns a canned response without actually making the request:
This mocked version can be used to validate the request-construction and response-parsing logic in the function under test without the expense of making live HTTP requests:
The use of ConfirmBy in the mock definition causes the evaluation to exit early and return a Failure object if the requested URL is incorrect:
External Links
Version History
Paclet Source
See Also