Mockingbird Documentation 0.18.0

Structure Value​Provider

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

init()

public init()  

Create an empty value provider.

Properties

collections​Provider

static let collectionsProvider  

A value provider with default-initialized collections.

https://developer.apple.com/documentation/foundation/collections

primitives​Provider

static let primitivesProvider  

A value provider with primitive Swift types.

https://developer.apple.com/documentation/foundation/numbers_data_and_basic_values

basics​Provider

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

strings​Provider

static let stringsProvider  

A value provider with string and text types.

https://developer.apple.com/documentation/foundation/strings_and_text

dates​Provider

static let datesProvider  

A value provider with date and time types.

https://developer.apple.com/documentation/foundation/dates_and_times

standard​Provider

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

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

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

value V

The value to register.

type K.​Type

The type to register the value under. value must be of kind type.

register​Type(_:​)

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

type T.​Type

A Providable type to register.

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

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

type T.​Type

A Providable type to remove.

provide​Value(for:​)

public func provideValue<T>(for type: T.Type = T.self) -> T?  

Provide a value for a given type.

Parameters

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.

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

lhs Self

A value provider.

rhs Self

A value provider.

Returns

A new value provider with the values of lhs and rhs.