Mockingbird Documentation 0.18.0

Operator ~>

infix operator ~>

The stubbing operator is used to bind an implementation to an intermediary Stub object.

Implementations

DynamicStubbingManager<ReturnType> ~> @escaping @autoclosure () -> ReturnType

public func ~> <ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping @autoclosure () -> ReturnType
)  

Stub a mocked method or property by returning a single value.

Stubbing allows you to define custom behavior for mocks to perform.

given(bird.doMethod()) ~> someValue
given(bird.property) ~> someValue

Match exact or wildcard argument values when stubbing methods with parameters. Stubs added later have a higher precedence, so add stubs with specific matchers last.

given(bird.canChirp(volume: any())) ~> true     // Any volume
given(bird.canChirp(volume: notNil())) ~> true  // Any non-nil volume
given(bird.canChirp(volume: 10)) ~> true        // Volume = 10

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping @autoclosure () -> Return​Type

A stubbed value to return.

DynamicStubbingManager<ReturnType> ~> @escaping () -> ReturnType

public func ~> <ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping () -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping () -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0) -> ReturnType

public func ~> <P0,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1) -> ReturnType

public func ~> <P0,P1,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2) -> ReturnType

public func ~> <P0,P1,P2,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3) -> ReturnType

public func ~> <P0,P1,P2,P3,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,P8,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping () throws -> ReturnType

public func ~> <ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping () throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping () throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0) throws -> ReturnType

public func ~> <P0,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1) throws -> ReturnType

public func ~> <P0,P1,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2) throws -> ReturnType

public func ~> <P0,P1,P2,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3) throws -> ReturnType

public func ~> <P0,P1,P2,P3,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,P8,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) throws -> Return​Type

A closure implementation stub to evaluate.

DynamicStubbingManager<ReturnType> ~> @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) throws -> ReturnType

public func ~> <P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,ReturnType>(
  manager: DynamicStubbingManager<ReturnType>,
  implementation: @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) throws -> ReturnType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Parameters

manager Dynamic​Stubbing​Manager<Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) throws -> Return​Type

A closure implementation stub to evaluate.

StaticStubbingManager<DeclarationType, InvocationType, ReturnType> ~> @escaping @autoclosure () -> ReturnType

public func ~> <DeclarationType: Declaration, InvocationType, ReturnType>(
  manager: StaticStubbingManager<DeclarationType, InvocationType, ReturnType>,
  implementation: @escaping @autoclosure () -> ReturnType
)  

Stub a mocked method or property by returning a single value.

Stubbing allows you to define custom behavior for mocks to perform.

given(bird.doMethod()) ~> someValue
given(bird.property) ~> someValue

Match exact or wildcard argument values when stubbing methods with parameters. Stubs added later have a higher precedence, so add stubs with specific matchers last.

given(bird.canChirp(volume: any())) ~> true     // Any volume
given(bird.canChirp(volume: notNil())) ~> true  // Any non-nil volume
given(bird.canChirp(volume: 10)) ~> true        // Volume = 10

Parameters

manager Static​Stubbing​Manager<Declaration​Type, Invocation​Type, Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation @escaping @autoclosure () -> Return​Type

A stubbed value to return.

StaticStubbingManager<DeclarationType, InvocationType, ReturnType> ~> InvocationType

public func ~> <DeclarationType: Declaration, InvocationType, ReturnType>(
  manager: StaticStubbingManager<DeclarationType, InvocationType, ReturnType>,
  implementation: InvocationType
)  

Stub a mocked method or property with a closure implementation.

Use a closure to implement stubs that contain logic, interact with arguments, or throw errors.

given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Stubs are type safe and work with inout and closure parameter types.

protocol Bird {
  func send(_ message: inout String)
  func fly(callback: (Result) -> Void)
}

// Inout parameter type
var message = "Hello!"
bird.send(&message)
print(message)   // Prints "HELLO!"

// Closure parameter type
given(bird.fly(callback: any())).will { callback in
  callback(.success)
}
bird.fly(callback: { result in
  print(result)  // Prints Result.success
})

Parameters

manager Static​Stubbing​Manager<Declaration​Type, Invocation​Type, Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

implementation Invocation​Type

A closure implementation stub to evaluate.

StubbingManager<DeclarationType, InvocationType, ReturnType> ~> ImplementationProvider<DeclarationType, InvocationType, ReturnType>

public func ~> <DeclarationType: Declaration, InvocationType, ReturnType>(
  manager: StubbingManager<DeclarationType, InvocationType, ReturnType>,
  provider: ImplementationProvider<DeclarationType, InvocationType, ReturnType>
)  

Stub a mocked method or property with an implementation provider.

There are several preset implementation providers such as lastSetValue, which can be used with property getters to automatically save and return values.

given(bird.name) ~> lastSetValue(initial: "")
print(bird.name)  // Prints ""
bird.name = "Ryan"
print(bird.name)  // Prints "Ryan"

Parameters

manager Stubbing​Manager<Declaration​Type, Invocation​Type, Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

provider Implementation​Provider<Declaration​Type, Invocation​Type, Return​Type>

An implementation provider that creates closure implementation stubs.

StubbingManager<DeclarationType, InvocationType, ReturnType> ~> ForwardingContext

public func ~> <DeclarationType: Declaration, InvocationType, ReturnType>(
  manager: StubbingManager<DeclarationType, InvocationType, ReturnType>,
  forwardingContext: ForwardingContext
)  

Stub a mocked method or property by forwarding invocations to a target.

Use the stubbing operator to bind a specific mocked declaration to a forwarding context.

class Crow {
  let name: String
  init(name: String) { self.name = name }
}

given(bird.name) ~> forward(to: Crow(name: "Ryan"))
print(bird.name)  // Prints "Ryan"

Parameters

manager Stubbing​Manager<Declaration​Type, Invocation​Type, Return​Type>

A stubbing manager containing declaration and argument metadata for stubbing.

forwarding​Context Forwarding​Context

A context that should receive forwarded invocations.