Function given(_:)
public func given<ReturnType>(
_ declaration: @autoclosure () throws -> ReturnType
) -> DynamicStubbingManager<ReturnType>
Stub a declaration 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 as Int < 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 as Int < 42
}
Properties can have stubs on both their getters and setters.
given(bird.name).willReturn("Ryan")
given(bird.name = any()).will { (name: String) in
print("Hello \(name)")
}
print(bird.name) // Prints "Ryan"
bird.name = "Sterling" // Prints "Hello Sterling"
Parameters
Name | Type | Description |
---|---|---|
declaration | @autoclosure () throws -> ReturnType |
A stubbable declaration. |