Class
StubbingManager
public class StubbingManager<DeclarationType: Declaration, InvocationType, ReturnType>
An intermediate object used for stubbing declarations returned by given
.
Relationships
Nested Types
StubbingManager.TransitionStrategy
When to use the next chained implementation provider.
Methods
willReturn(_:)
@discardableResult
public func willReturn(_ value: ReturnType) -> Self
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()).willReturn(someValue)
given(bird.getProperty()).willReturn(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())).willReturn(true) // Any volume
given(bird.canChirp(volume: notNil())).willReturn(true) // Any non-nil volume
given(bird.canChirp(volume: 10)).willReturn(true) // Volume = 10
Parameters
Name | Type | Description |
---|---|---|
value | ReturnType |
A stubbed value to return. |
Returns
The current stubbing manager which can be used to chain additional stubs.
willReturn(_:transition:)
@discardableResult
public func willReturn(
_ provider: ImplementationProvider<DeclarationType, InvocationType, ReturnType>,
transition: TransitionStrategy = .onFirstNil
) -> Self
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.getName()).willReturn(lastSetValue(initial: ""))
print(bird.name) // Prints ""
bird.name = "Ryan"
print(bird.name) // Prints "Ryan"
Implementation providers usually return multiple values, so when using chained stubbing it's necessary to specify a transition strategy that defines when to go to the next stub.
given(bird.getName())
.willReturn(lastSetValue(initial: ""), transition: .after(2))
.willReturn("Sterling")
print(bird.name) // Prints ""
bird.name = "Ryan"
print(bird.name) // Prints "Ryan"
print(bird.name) // Prints "Sterling"
Parameters
Name | Type | Description |
---|---|---|
provider | ImplementationProvider<DeclarationType, InvocationType, ReturnType> |
An implementation provider that creates closure implementation stubs. |
transition | TransitionStrategy |
When to use the next implementation provider in the list. |
Returns
The current stubbing manager which can be used to chain additional stubs.
will(_:)
@discardableResult
public func will(_ implementation: InvocationType) -> Self
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()))
.will { 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 |
---|---|---|
implementation | InvocationType |
A closure implementation stub to evaluate. |
Returns
The current stubbing manager which can be used to chain additional stubs.