Mockingbird Documentation 0.15.0

Function use​Default​Values(from:​on:​)

public func useDefaultValues(from valueProvider: ValueProvider, on mocks: [Mock])

Start returning default values for unstubbed methods on multiple mocks.

Mocks are strict by default, meaning that calls to unstubbed methods will trigger a test failure. Methods returning Void do not need to be stubbed in strict mode.

let bird = mock(Bird.self)
print(bird.name)  // Fails because `bird.getName()` is not stubbed
bird.fly()        // Okay because `fly()` has a `Void` return type

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 anotherBird = mock(Bird.self)
useDefaultValues(from: .standardProvider, on: [bird, anotherBird])
print(bird.name)  // Prints ""
print(anotherBird.name)  // Prints ""

You can create custom value providers by registering values for types. See Providable for how to provide "wildcard" instances for generic types.

var valueProvider = ValueProvider(from: .standardProvider)
valueProvider.register("Ryan", for: String.self)

useDefaultValues(from: valueProvider, on: [bird, anotherBird])

print(bird.name)  // Prints "Ryan"
print(anotherBird.name)  // Prints "Ryan"

Values from concrete stubs always have a higher precedence than default values.

given(bird.getName()) ~> "Ryan"
print(bird.name)  // Prints "Ryan"

useDefaultValues(from: .standardProvider, on: [bird, anotherBird])

print(bird.name)  // Prints "Ryan"
print(anotherBird.name)  // Prints ""

Parameters

value​Provider Value​Provider

A value provider to add.

mocks [Mock]

A list of mocks that should start using the value provider.