Structure
ValueProvider
public struct ValueProvider
Provides concrete instances of types.
To return default values for unstubbed methods, use a ValueProvider
with the initialized mock.
Mockingbird provides preset value providers which are guaranteed to be backwards compatible,
such as .standardProvider
.
let bird = mock(Bird.self)
bird.useDefaultValues(from: .standardProvider)
print(bird.name) // Prints ""
You can create custom value providers by registering values for types.
var valueProvider = ValueProvider()
valueProvider.register("Ryan", for: String.self)
bird.useDefaultValues(from: valueProvider)
print(bird.name) // Prints "Ryan"
Initializers
Properties
collectionsProvider
static let collectionsProvider
A value provider with default-initialized collections.
https://developer.apple.com/documentation/foundation/collections
primitivesProvider
static let primitivesProvider
A value provider with primitive Swift types.
https://developer.apple.com/documentation/foundation/numbers_data_and_basic_values
basicsProvider
static let basicsProvider
A value provider with basic number and data types that are not primitives.
https://developer.apple.com/documentation/foundation/numbers_data_and_basic_values
stringsProvider
static let stringsProvider
A value provider with string and text types.
https://developer.apple.com/documentation/foundation/strings_and_text
datesProvider
static let datesProvider
A value provider with date and time types.
https://developer.apple.com/documentation/foundation/dates_and_times
standardProvider
public static let standardProvider = ValueProvider() +
.collectionsProvider +
.primitivesProvider +
.basicsProvider +
.stringsProvider +
.datesProvider
All preset value providers.
Methods
add(_:)
mutating public func add(_ other: Self)
Adds the values from another value provider.
Value providers can be composed by adding values from another provider. Values in the other provider have precedence and will overwrite existing values in this provider.
var valueProvider = ValueProvider()
valueProvider.add(.standardProvider)
print(valueProvider.provideValue(for: String.self)) // Prints ""
Parameters
Name | Type | Description |
---|---|---|
other | Self |
A value provider to combine. |
adding(_:)
public func adding(_ other: Self) -> Self
Returns a new value provider containing the values from both providers.
Value providers can be composed by adding values from another provider. Values in the added provider have precendence over those in base provider.
let valueProvider = ValueProvider.collectionsProvider.adding(.primitivesProvider)
print(valueProvider.provideValue(for: [Bool].self)) // Prints []
print(valueProvider.provideValue(for: Int.self)) // Prints 0
Parameters
Name | Type | Description |
---|---|---|
other | Self |
A value provider to combine. |
Returns
A new value provider with the values of lhs
and rhs
.
register(_:for:)
mutating public func register<K, V>(_ value: V, for type: K.Type)
Register a value for a specific type.
Create custom value providers by registering values for types.
var valueProvider = ValueProvider()
valueProvider.register("Ryan", for: String.self)
print(valueProvider.provideValue(for: String.self)) // Prints "Ryan"
Parameters
Name | Type | Description |
---|---|---|
value | V |
The value to register. |
type | K.Type |
The type to register the value under. |
registerType(_:)
mutating public func registerType<T: Providable>(_ type: T.Type = T.self)
Register a Providable
type used to provide values for generic types.
Provide wildcard instances for generic types by conforming the base type to Providable
and
registering the type. Non-wildcard instances have precedence over wildcard instances.
extension Array: Providable {
public static func createInstance() -> Self? {
return Array()
}
}
var valueProvider = ValueProvider()
valueProvider.registerType(Array<Any>.self)
// All specializations of `Array` return an empty array
print(valueProvider.provideValue(for: Array<String>.self)) // Prints []
print(valueProvider.provideValue(for: Array<Data>.self)) // Prints []
// Register a non-wildcard instance of `Array<String>`
valueProvider.register(["A", "B"], for: Array<String>.self)
print(valueProvider.provideValue(for: Array<String>.self)) // Prints ["A", "B"]
print(valueProvider.provideValue(for: Array<Data>.self)) // Prints []
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
A |
remove(_:)
mutating public func remove<T>(_ type: T.Type)
Remove a registered value for a given type.
Previously registered values can be removed from the top-level value provider. This does not affect values provided by subproviders.
var valueProvider = ValueProvider(from: .standardProvider)
print(valueProvider.provideValue(for: String.self)) // Prints ""
// Override the subprovider value
valueProvider.register("Ryan", for: String.self)
print(valueProvider.provideValue(for: String.self)) // Prints "Ryan"
// Remove the registered value
valueProvider.remove(String.self)
print(valueProvider.provideValue(for: String.self)) // Prints ""
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
The type to remove a previously registered value for. |
remove(_:)
mutating public func remove<T: Providable>(_ type: T.Type = T.self)
Remove a registered Providable
type.
Previously registered wildcard instances for generic types can be removed from the top-level value provider.
var valueProvider = ValueProvider()
valueProvider.registerType(Array<Any>.self)
print(valueProvider.provideValue(for: Array<String>.self)) // Prints []
valueProvider.remove(Array<Any>.self)
print(valueProvider.provideValue(for: Array<String>.self)) // Prints nil
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
A |
provideValue(for:)
public func provideValue<T>(for type: T.Type = T.self) -> T?
Provide a value for a given type.
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
A type to provide a value for. |
Returns
A concrete instance of the given type, or nil
if no value could be provided.
provideValue(for:)
public func provideValue<T: Providable>(for type: T.Type = T.self) -> T?
Provide a value a given Providable
type.
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
A |
Returns
A concrete instance of the given type, or nil
if no value could be provided.
Operators
+
static public func + (lhs: Self, rhs: Self) -> Self
Returns a new value provider containing the values from both providers.
Value providers can be composed by adding values from other providers. Values in the second provider have precendence over those in first provider.
let valueProvider = .collectionsProvider + .primitivesProvider
print(valueProvider.provideValue(for: [Bool].self)) // Prints []
print(valueProvider.provideValue(for: Int.self)) // Prints 0
Parameters
Name | Type | Description |
---|---|---|
lhs | Self |
A value provider. |
rhs | Self |
A value provider. |
Returns
A new value provider with the values of lhs
and rhs
.