Mockingbird Documentation 0.18.0

Function not​Nil(_:​)

public func notNil<T: NSObjectProtocol>(_ type: T.Type = T.self) -> T  

Matches any non-nil Objective-C object argument value.

Argument matching allows you to stub or verify specific invocations of parameterized methods. Use the argument matcher notNil to match non-nil argument values.

// Protocol referencing Obj-C object types
protocol Bird {
  func send(_ message: NSString?)
}

given(bird.send(notNil())).will { print($0) }

bird.send("Hello")  // Prints Optional("Hello")
bird.send(nil)      // Error: Missing stubbed implementation

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

// Protocol referencing Obj-C object types
protocol Bird {
  func send<T: NSObject>(_ message: T?)  // Overloaded generically
  func send(_ message: NSString?)        // Overloaded explicitly
  func send(_ messages: NSData?)
}

given(bird.send(notNil(NSString?.self)))
  .will { print($0) }

bird.send("Hello")  // Prints Optional("Hello")
bird.send(nil)      // Error: Missing stubbed implementation

Parameters

type T.​Type

The parameter type used to disambiguate overloaded methods.