std.unittest.mock Package
Function Description
The unittest.mock package provides a mock framework for Cangjie unit tests and provides APIs for creating and configuring mock objects. These mock objects have the same APIs as the real objects. The mock test technology supports testing code in isolation. Test cases are encoded using mock objects to eliminate external dependencies.
The mock framework has the following features:
- Creation of mock and spy objects: Production code does not need to be modified during tests.
- Simple configuration API: The behavior of mock/spy objects can be configured.
- Unit test framework: Other features of the unit test framework are seamlessly integrated. The error output is readable.
- Automatic verification of configuration behavior: In most cases, extra verification code is not needed.
- Verification API: This API is used to test complex interactions within the system.
The application scenarios are as follows:
- simplifying test settings and code;
- testing abnormal scenarios;
- replacing costly dependencies with lightweight mock objects to improve test performance; and
- verifying and testing complex scenarios, for example, sequence or number of calls.
You can compile the first mock test program by referring to Getting Started with the Mock Framework. In addition, Basic Concepts and Usage of Mock are described together with example code. Advanced use of APIs (stub) is further illustrated.
API List
Function
Name | Description |
---|---|
mock<T>() | Creates a mock object of type T. By default, all member functions, attributes, or operator overloading functions of this object do not have any specific implementation. |
mock<T>(Array<StubMode>) | Creates a mock object of type T. A parameter specifies the stub mode. |
spy<T>(T) | Creates a spy object of type T (an extension of mock object ). The members of the object have a skeleton object implemented by default. This object wraps the input object. In addition, the member functions, attributes, or operator overloading functions are implemented as calls to the corresponding member functions, attributes, or operator overloading functions of the input instance object. |
API
Name | Description |
---|---|
ValueListener<T> | Provides multiple member functions to support "listening" on the parameters input in the stub signature. |
Class
Name | Description |
---|---|
ActionSelector | Provides methods for specifying an operation API for member functions and permitting chain calls. |
AnyMatcher | Specifies an arbitrary parameter matcher, that is, the stub signature permits any parameter. |
ArgumentMatcher | Specifies an abstract class of the parameter matcher. This class and its subclasses can be used as the input parameter types of the stub signature. |
CardinalitySelector | Provides the API for defining the number of executions of the latest behavior of a stub signature. |
ConfigureMock | Configures mock object . |
Continuation | Provides the API for continuing to define the behavior of a stub signature. |
GetterActionSelector | Provides methods for specifying an operation API for the Getter function and permitting chain calls. |
Matchers | Provides the static function for generating a matcher. The matcher object can be generated only by this static function. Matchers can be used in the stub chain. |
MethodActionSelector | Provides methods for specifying an operation API for member functions and permitting chain calls. |
MockFramework | Provides functions for preparing and recycling the framework required for use case execution. |
NoneMatcher | Specifies the matcher with the parameter value of None . |
OrderedVerifier | Specifies the class that is used to collect verification statements and can dynamically input verification behavior in the ordered function. |
SetterActionSelector | Provides methods for specifying an operation API for the Setter function and permitting chain calls. |
SyntheticField | Specifies a synthetic field. |
TypedMatcher | Specifies a parameter type matcher. |
UnorderedVerifier | Specifies the class that is used to collect verification statements and can dynamically input verification behavior in the unordered function. |
Verify | Provides a series of static methods to support the definition of verification actions such as that , ordered , and unorder . |
VerifyStatement | Specifies a single verification statement (that is, the foregoing verification statement) for the stub signature within the verification scope, and provides the number of executions of the stub signature specified by a member function. |
Enumeration
Name | Description |
---|---|
Exhaustiveness | Specifies the verification mode of the unordered function. Two modes are available. |
MockSessionKind | Specifies the type of a stub that can be used in MockSession. |
StubMode | Controls the stub mode. |