StaticMock

public protocol StaticMock : AnyObject

Every mock, that stubs static methods, should adopt StaticMock protocol. It defines base StaticMock structure and features.

  • Stubbed method and property type

    Declaration

    Swift

    associatedtype StaticGiven
  • Verification type

    Declaration

    Swift

    associatedtype StaticVerify
  • Perform type

    Declaration

    Swift

    associatedtype StaticPerform
  • Registers return value for stubbed method, for specified attributes set.

    When this method will be called on mock, it will check for first matching given, with following rules:

    1. First check most specific givens (with explicit parameters - .value), then for wildcard parameters (.any)
    2. More recent givens have higher priority than older ones
    3. When two given’s have same level of explicity, like: Given(mock, .do(with: .value(1), and: .any) Given(mock, .do(with: .any, and: .value(1)) Method stub will return most recent one.

    Declaration

    Swift

    static func given(_ method: StaticGiven)

    Parameters

    method

    signature, with attributes (any or explicit value). Type . for all available

  • Registers perform closure, which will be executed upon calling stubbed method, for specified attribtes.

    When this method will be called on mock, it will check for first matching closure and execute it with parameters passed. Have in mind following rules:

    1. First check most specific performs (with explicit parameters - .value), then for wildcard parameters (.any)
    2. More recent performs have higher priority than older ones
    3. When two performs have same level of explicity, like: Perform(mock, .do(with: .value(1), and: .any, perform: { ... })) Perform(mock, .do(with: .any, and: .value(1), perform: { ... })) Method stub will return most recent one.

    Declaration

    Swift

    static func perform(_ method: StaticPerform)

    Parameters

    method

    signature, with attributes (any or explicit value). Type . for all available

  • Verifies, that given method stub was called exact number of times.

    Declaration

    Swift

    static func verify(_ method: StaticVerify, count: Count, file: StaticString, line: UInt)

    Parameters

    method

    Method signature with wrapped parameters (Parameter)

    count

    Number of invocations

    file

    for XCTest print purposes

    line

    for XCTest print purposes

  • Clear mock internals. You can specify what to clear (invocations aka verify, givens or performs) or leave it empty to clear all mock internals.

    Example:

    mock.resetMock(.invocation)         // clear invocations, Verify will have all the count on 0
    mock.resetMock(.given, .perform)    // clear only mock setup, invocations stays
    mock.resetMock()                    // clear all
    

    Declaration

    Swift

    static func resetMock(_ scopes: MockScope...)