Function any(_:of:)
public func any<T: AnyObject>(_ type: T.Type = T.self, of objects: T...) -> T
Matches argument values identical to any of the provided values.
Argument matching allows you to stub or verify specific invocations of parameterized methods.
Use the argument matcher any(of:)
to match objects identical to one or more of the specified
values.
// Reference type
class Location {
let name: String
init(name: String) { self.name = name }
}
protocol Bird {
func fly(to location: Location)
}
let home = Location(name: "Home")
let work = Location("Work")
given(bird.fly(to: any(of: home, work)))
.will { print($0.name) }
bird.fly(to: home) // Prints "Home"
bird.fly(to: work) // Prints "Work"
let hawaii = Location("Hawaii")
bird.fly(to: hawaii)) // Error: Missing stubbed implementation
Methods overloaded by parameter type can be disambiguated by explicitly specifying the type.
protocol Bird {
func fly<T>(to location: T) // Overloaded generically
func fly(to location: Location) // Overloaded explicitly
func fly(to locationName: String)
}
given(bird.fly(to: any(String.self, of: "Home", "Work")))
.will { print($0) }
bird.send("Home") // Prints "Hi"
bird.send("Work") // Prints "Hello"
bird.send("Hawaii") // Error: Missing stubbed implementation
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
The parameter type used to disambiguate overloaded methods. |
objects | T |
A set of non-equatable objects that should result in a match. |