Mock

public protocol Mock : AnyObject

Every generated mock implementation adopts Mock protocol. It defines base Mock structure and features.

  • Stubbed method and property type

    Declaration

    Swift

    associatedtype Given
  • Verification type

    Declaration

    Swift

    associatedtype Verify
  • Perform type

    Declaration

    Swift

    associatedtype Perform
  • 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

    func given(_ method: Given)

    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

    func perform(_ method: Perform)

    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

    func verify(_ method: Verify, 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

    func resetMock(_ scopes: MockScope...)