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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping @autoclosure () -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping () -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping () throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | DynamicStubbingManager<ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping (P0,P1,P2,P3,P4,P5,P6,P7,P8,P9) throws -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | StaticStubbingManager<DeclarationType, InvocationType, ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | @escaping @autoclosure () -> ReturnType |
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
Name | Type | Description |
---|---|---|
manager | StaticStubbingManager<DeclarationType, InvocationType, ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
implementation | InvocationType |
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
Name | Type | Description |
---|---|---|
manager | StubbingManager<DeclarationType, InvocationType, ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
provider | ImplementationProvider<DeclarationType, InvocationType, ReturnType> |
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
Name | Type | Description |
---|---|---|
manager | StubbingManager<DeclarationType, InvocationType, ReturnType> |
A stubbing manager containing declaration and argument metadata for stubbing. |
forwardingContext | ForwardingContext |
A context that should receive forwarded invocations. |