Mockingbird Documentation 0.16.0

Function mock(_:​)

@available(*, unavailable, message: "No generated mock for this type which might be resolved by building the test target (⇧⌘U)") public func mock<T>(_ type: T.Type) -> T

Returns a mock of a given type.

Initialized mocks can be passed in place of the original type. Protocol mocks do not require explicit initialization while class mocks should be created using initialize(…).

protocol Bird {
  init(name: String)
}
class Tree {
  init(with bird: Bird) {}
}

let bird = mock(Bird.self)  // Protocol mock
let tree = mock(Tree.self).initialize(with: bird)  // Class mock

Generated mock types are suffixed with Mock and should not be coerced into their supertype.

let bird: BirdMock = mock(Bird.self)  // The concrete type is `BirdMock`
let inferredBird = mock(Bird.self)    // Type inference also works
let coerced: Bird = mock(Bird.self)   // Avoid upcasting mocks

Parameters

type T.​Type

The type to mock.