Mockingbird Documentation 0.18.0

Function forward(to:​)

public func forward<T>(to object: T) -> ForwardingContext  

Forward calls for a specific declaration to an object.

Objects are strongly referenced and receive proxed invocations until removed with clearStubs. Targets added afterwards have a higher precedence and only pass calls down the forwarding chain if unable handle the invocation, such as when the target is unrelated to the mocked type.

class Crow: Bird {
  let name: String
  init(name: String) { self.name = name }
}

given(bird.name) ~> forward(to: Crow(name: "Ryan"))
print(bird.name)  // Prints "Ryan"

// Additional targets take precedence
given(bird.name) ~> forward(to: Crow(name: "Sterling"))
print(bird.name)  // Prints "Sterling"

Concrete stubs always have a higher priority than forwarding targets, regardless of the order they were added.

given(bird.name) ~> "Ryan"
given(bird.name) ~> forward(to: Crow(name: "Sterling"))
print(bird.name)  // Prints "Ryan"

Objects must inherit from the mocked type to handle forwarded invocations, even if the declaration is identical. Adding an unrelated type as a forwarding target is a no-op.

// Not a `Bird`
class Person {
  var name = "Ryan"
}

given(bird.name) ~> forward(to: Person())
print(bird.name)  // Error: Missing stubbed implementation

Parameters

object T

An object that should handle forwarded invocations.