Home

Awesome

<p align="center"> <img src ="Resources/Logo_Rounded.png" alt="MockingKit Logo" title="MockingKit" /> </p> <p align="center"> <img src="https://img.shields.io/github/v/release/danielsaidi/MockingKit?color=%2300550&sort=semver" alt="Version" /> <img src="https://img.shields.io/badge/Swift-6.0-orange.svg" alt="Swift 6.0" /> <img src="https://img.shields.io/github/license/danielsaidi/MockingKit" alt="MIT License" /> <a href="https://twitter.com/danielsaidi"><img src="https://img.shields.io/twitter/url?label=Twitter&style=social&url=https%3A%2F%2Ftwitter.com%2Fdanielsaidi" alt="Twitter: @danielsaidi" title="Twitter: @danielsaidi" /></a> <a href="https://mastodon.social/@danielsaidi"><img src="https://img.shields.io/mastodon/follow/000253346?label=mastodon&style=social" alt="Mastodon: @danielsaidi@mastodon.social" title="Mastodon: @danielsaidi@mastodon.social" /></a> </p>

About MockingKit

MockingKit is a Swift SDK that lets you easily mock protocols and classes.

MockingKit lets you create mocks of any protocol or class, after which you can call functions, register dynamic function results, automatically record method invocations, and inspect all 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.

Installation

MockingKit can be installed with the Swift Package Manager:

https://github.com/danielsaidi/MockingKit.git

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                                   // => (42, "string")
calls[0].result                                      // => "gnirts"
mock.hasCalled(\.doStuffRef)                         // => true

See the online getting started guide for more information.

Documentation

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

Demo Application

The Demo folder has an app that lets you explore the library and see how mocks behave.

Support my work

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

Your support makes it possible for me to put more work into these projects and make them the best they can be.

Contact

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

License

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