MockingKit is a Swift-based library that lets you mock protocols and classes. It lets you register function results, invoke and inspect method calls etc.

What's New


This version bumps Swift to 5.9.

MockingKit Logo

Version Swift 5.9 Swift UI MIT License Twitter: @danielsaidi Mastodon:

About MockingKit

MockingKit lets you mock protocols and classes in Swift. This is useful when unit testing, or to fake functionality that is not yet implemented.

MockingKit lets you create mocks of any protocol or open class, after which you can call functions, register results, record method invocations, and inspect recorded calls.

MockingKit doesn't require any setup or build scripts, and puts no restrictions on your code or architecture. Just create a mock and you're good to go.


MockingKit can be installed with the Swift Package Manager:

If you prefer to not have external dependencies, you can also just copy the source code into your app.

Getting started

MockingKit lets you create mocks of any protocol or open class.

For instance, consider this simple protocol:

protocol MyProtocol {

    func doStuff(int: Int, string: String) -> String

With MockingKit, you can easily create a mock implementation of this protocol:

import MockingKit

class MyMock: Mock, MyProtocol {

    // 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 now use the mock to register function results, call functions and inspect recorded calls.

// Create a mock instance
let mock = MyMock()

// Register a result to be returned by doStuff
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: \.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

To mock a class, you just have to subclass the class and implement the Mockable protocol:

import MockingKit

class MockUserDefaults: UserDefaults, Mockable {

    // You must provide a mock when implementing Mockable
    var mock = Mock()

    // You can now create lazy references just like in the protocol mock above

For more information, please see the getting started guide.


The online documentation has more information, code examples, etc.

Demo Application

The demo app lets you explore the library on iOS and macOS. To try it out, just open and run the Demo project.

Support my work

You can sponsor me on GitHub Sponsors or reach out for paid support, to help support my open-source projects.


Feel free to reach out if you have questions or if you want to contribute in any way:


MockingKit is available under the MIT license. See the LICENSE file for more info.


  • Swift Tools 5.9.0
View More Packages from this Author


  • None
Last updated: Mon Jan 15 2024 17:27:32 GMT-1000 (Hawaii-Aleutian Standard Time)