Mockingbird Documentation 0.17.0

Protocol Mock

public protocol Mock  

All generated mocks conform to this protocol.

Mock Mock StaticMock StaticMock StaticMock->Mock

Types Conforming to Mock

StaticMock

Used to store invocations on static or class scoped methods.

Default Implementations

use​Default​Values(from:​)

@discardableResult
  func useDefaultValues(from valueProvider: ValueProvider) -> Self  

Adds a value provider returning default values for unstubbed methods to this mock.

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.

bird.useDefaultValues(from: .standardProvider)
print(bird.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)
bird.useDefaultValues(from: valueProvider)
print(bird.name)  // Prints "Ryan"

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

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

bird.useDefaultValues(from: .standardProvider)
print(bird.name)  // Prints "Ryan"

Parameters

value​Provider Value​Provider

A value provider to add.

Requirements

mocking​Context

var mockingContext: MockingContext  

Information about received invocations.

stubbing​Context

var stubbingContext: StubbingContext  

Implementations for stubbing behaviors.

mock​Metadata

var mockMetadata: MockMetadata  

Static metadata about the mock created at generation time.

source​Location

var sourceLocation: SourceLocation?  

Where the mock was initialized.