Mockingbird Documentation 0.15.0

Function given(_:​)

public func given<DeclarationType: Declaration, InvocationType, ReturnType>(_ declarations: Mockable<DeclarationType, InvocationType, ReturnType>) -> StubbingManager<DeclarationType, InvocationType, ReturnType>

Stub one or more declarations to return a value or perform an operation.

Stubbing allows you to define custom behavior for mocks to perform.

given(bird.canChirp()).willReturn(true)
given(bird.canChirp()).willThrow(BirdError())
given(bird.canChirp(volume: any())).will { volume in
  return volume < 42
}

This is equivalent to the shorthand syntax using the stubbing operator ~>.

given(bird.canChirp()) ~> true
given(bird.canChirp()) ~> { throw BirdError() }
given(bird.canChirp(volume: any())) ~> { volume in
  return volume < 42
}

Properties can be stubbed with their getter and setter methods.

given(bird.getName()).willReturn("Ryan")
given(bird.setName(any())).will { print($0) }

Parameters

declarations Mockable<Declaration​Type, Invocation​Type, Return​Type>

One or more stubbable declarations.