Mockingbird Swift Documentation

website·Indexed 31 days ago

https://mockingbirdswift.com

API documentation for Mockingbird, a mocking framework for Swift. Includes details on InvocationRecorder, StubbingManager, ValueProvider, and verification options.

Tokens
60K
Snippets
525
Records
589
Agent score
58%

What's inside Mockingbird

  1. Overview of Mockingbird Framework

    Mockingbird is a mocking framework for Swift and Objective-C designed to eliminate boilerplate and the need to modify production code for testing. It provides capabilities for mocking, stubbing, and verifying objects, including advanced features like partial mocks, call order verification, and asynchronous code testing.

    Technical implementation:

    • Swift types: Statically mocked at compile time via codegen.
    • Objective-C types: Dynamically mocked at run time using NSProxy.
  2. Overview of Mockingbird Internal APIs

    Mockingbird provides a set of internal APIs used for mocking, stubbing, verification, and runtime management. These APIs are not designed for general use by end-users but form the foundation of the framework's functionality.
  3. Access invocation information using MockingContext

    The mocking property provides a MockingContext that contains information about received invocations on a mock object, allowing developers to verify calls and inspect arguments.
    @objc let mocking: MockingContext
  4. Use InvocationRecorder to record mock invocations

    The InvocationRecorder class records invocations for stubbing and verification purposes. It provides a global shared instance via sharedRecorder to manage these recordings across the test environment.
    // Access the global invocation recorder
    let recorder = InvocationRecorder.sharedRecorder
  5. Use StaticStubbingManager to stub Swift declarations

    StaticStubbingManager is an intermediate object used in Mockingbird to stub Swift declarations that are returned by the given function. It inherits from StubbingManager and is used in conjunction with the stubbing operator (~>) to define the behavior of mocked methods or properties.
    class StaticStubbingManager<DeclarationType, InvocationType, ReturnType> where DeclarationType : Declaration
  6. Use the Synchronized class for thread-safe data access

    The Synchronized<T> class is a wrapper used to ensure thread-safe access to a value of type T. It provides mechanisms to read the value or update it atomically using closures.
    class Synchronized<T>
  7. Use StubbingContext to manage mock implementations

    The StubbingContext class (exposed to Objective-C as MKBStubbingContext) is used by Mockingbird to store stubbed implementations that mocks use to determine their behavior. It provides mechanisms to evaluate return values for Objective-C invocations and handle default value provision.
    @objc(MKBStubbingContext) class StubbingContext
  8. Use ForwardingContext to bind forwarding targets to a mock

    ForwardingContext is a structure used as an intermediary object to bind forwarding targets to a mock in Mockingbird.
    struct ForwardingContext
  9. Use StubbingManager to define mock behavior

    StubbingManager is an intermediate object used to stub declarations returned by the given function. It provides a fluent API to specify what a mocked method or property should do when invoked during a test.
    class StubbingManager<DeclarationType, InvocationType, ReturnType> where DeclarationType : Declaration
  10. Access mock state via Context instance properties

    The Context class provides three primary properties to manage different aspects of a mock's lifecycle:

    • mocking: A MockingContext containing information about received invocations.
    • proxy: A ProxyContext managing the invocation handler chain.
    • stubbing: A StubbingContext used to define implementations for stubbing behaviors.
  11. Access runtime metadata via mockingbirdContext

    The mockingbirdContext property is a required instance property on Mockingbird mock objects. It provides access to the Context object containing runtime metadata about the specific mock instance.
    var mockingbirdContext: Context { get }
  12. Use Mockingbird starter pack for system framework compatibility

    Mockingbird includes a starter pack of supporting source files to provide basic compatibility with the Swift standard library and common system frameworks. These are automatically downloaded and integrated by the configurator, but the latest versions can be manually retrieved from the GitHub release artifacts.