Mockingbird Documentation 0.16.0

Function any(_:​keys:​)

public func any<K, V>(_ type: Dictionary<K, V>.Type = Dictionary<K, V>.self, keys: K) -> Dictionary<K, V>

Matches any dictionary containing all of the keys.

Argument matching allows you to stub or verify specific invocations of parameterized methods. Use the argument matcher any(keys:) to match dictionaries that contain all specified keys.

protocol Bird {
  func send(_ messages: [UUID: String])
}

let messageId1 = UUID()
let messageId2 = UUID()
given(bird.send(any(containing: messageId1, messageId2)))
  .will { print($0) }

bird.send([
  messageId1: "Hi",
  messageId2: "Hello",
])  // Prints ["Hi", "Hello"]

bird.send([
  UUID(): "Hi",
  UUID(): "Hello",
])  // Error: Missing stubbed implementation

Methods overloaded by parameter type can be disambiguated by explicitly specifying the type.

protocol Bird {
  func send<T>(_ messages: [UUID: T])    // Overloaded generically
  func send(_ messages: [UUID: String])  // Overloaded explicitly
  func send(_ messages: [UUID: Data])
}

let messageId1 = UUID()
let messageId2 = UUID()
given(bird.send(any([UUID: String].self, containing: messageId1, messageId2)))
  .will { print($0) }

bird.send([
  messageId1: "Hi",
  messageId2: "Hello",
])  // Prints ["Hi", "Hello"]

bird.send([
  messageId1: Data([1]),
  messageId2: Data([2]),
])  // Error: Missing stubbed implementation

Parameters

type Dictionary<K, V>.​Type

The parameter type used to disambiguate overloaded methods.

keys K

A set of keys that must all exist in the dictionary to match.