About MockingKit
MockingKit is a Swift-based mocking library that makes it easy to mock protocols and classes, for instance when unit testing or mocking not yet implemented functionality.
MockingKit lets you register
function results, call
functions and inspect
recorded calls.
MockingKit doesn't put any restrictions on your code or require you to structure it in any way. You don't need any setup or configuration. Just create a mock and you're good to go.
Supported Platforms
MockingKit supports iOS 13
, macOS 10.15
, tvOS 13
and watchOS 6
.
Installation
MockingKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/MockingKit.git
or with CocoaPods:
pod MockingKit
You can also clone the repository and build the library locally.
Getting started
In short, MockingKit lets you mock any protocols and classes:
protocol MyProtocol {
func doStuff(int: Int, string: String) -> String
}
class MyMock: Mock, MyProtocol {
// You have to define a lazy reference for each function you want to mock
lazy var doStuffRef = MockReference(doStuff)
// Functions must then call the reference to be recorded
func doStuff(int: Int, string: String) -> String {
call(doStuffRef, args: (int, string))
}
}
You can then use the mock to register
function results, call
functions and inspect
recorded calls.
// Create a mock
let mock = MyMock()
// Register a doStuff result
mock.registerResult(for: mock.doStuffRef) { args in String(args.1.reversed()) }
// Calling doStuff will now return the pre-registered result
let result = mock.doStuff(int: 42, string: "string") // => "gnirts"
// You can now inspect calls made to doStuff
let calls = mock.calls(to: mock.doStuffRef) // => 1 item
calls[0].arguments.0 // => 42
calls[0].arguments.1 // => "string"
calls[0].result // => "gnirts"
mock.hasCalled(\.doStuffRef) // => true
mock.hasCalled(\.doStuffRef, numberOfTimes: 1) // => true
mock.hasCalled(\.doStuffRef, numberOfTimes: 2) // => false
The online documentation has a more thorough getting-started guide that will help you get started with the library.
Documentation
The online documentation has articles, code examples etc. that let you overview the various parts of the library.
The online documentation is currently iOS-specific. To generate documentation for other platforms, open the package in Xcode, select a simulator then run Product/Build Documentation
.
Demo Application
This project contains a demo app that lets you explore MockingKit on iOS and macOS. To run it, just open and run Demo/Demo.xcodeproj
.
Support
You can sponsor this project on GitHub Sponsors or get in touch for paid support.
Contact
Feel free to reach out if you have questions or if you want to contribute in any way:
- E-mail: [email protected]
- Twitter: @danielsaidi
- Web site: danielsaidi.com
License
MockingKit is available under the MIT license. See the LICENSE file for more info.