Function mock(_:)
public func mock<T: NSObjectProtocol>(_ type: T.Type) -> T
Returns a dynamic mock of a given Objective-C object type.
Initialized mocks can be passed in place of the original type. Dynamic mocks use the Objective-C runtime and do not require explicit initialization like Swift class mocks.
// Objective-C declarations
@protocol Bird <NSObject>
- (instancetype)initWithName:(NSString *);
@end
@interface Tree : NSObject
- (instancetype)initWithHeight:(NSInteger)height;
@end
let bird = mock(Bird.self) // Protocol mock
let tree = mock(Tree.self) // Class mock
It's also possible to mock Swift types inheriting from NSObject
or conforming to
NSObjectProtocol
. Members must be dynamically dispatched and available to the Objective-C
runtime by specifying the objc
attribute and dynamic
modifier.
@objc protocol Bird: NSObjectProtocol {
@objc dynamic func chirp()
@objc dynamic var name: String { get }
}
@objc class Tree: NSObject {
@objc dynamic func shake() {}
@objc dynamic var height: Int
}
Parameters
Name | Type | Description |
---|---|---|
type | T.Type |
The type to mock. |