Skip to content

MockingKit is a Swift mocking library that makes it easy to mock protocol implementations for unit tests and not yet implemented functionality. It lets you register function results, invoke method calls and inspect invokations.

License

Notifications You must be signed in to change notification settings

BookBeat/MockingKit

 
 

Repository files navigation

MockingKit Logo

Version Swift 5.6 Swift UI MIT License Twitter: @danielsaidi Mastodon: @danielsaidi@mastodon.social

About MockingKit

MockingKit is a Swift library that lets you mock protocols and classes, for instance when unit testing or mocking not yet implemented functionality.

MockingKit lets you create mock implementations of any protocol or mock sub classes any open base class, after which you can call functions, register function results and inspect all recorded calls.

MockingKit doesn't require any project configurations or build scripts, put any restrictions on your code or require you to structure it in any way. 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

or with CocoaPods:

pod MockingKit

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

Supported Platforms

MockingKit supports iOS 13, macOS 10.15, tvOS 13 and watchOS 6.

Getting started

The online documentation has a getting started guide guide to help you get started with MockingKit.

In short, MockingKit lets you mock any protocols and open classes. 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 {

    // 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 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

For more information, please see the online documentation and getting started guide.

Documentation

The online documentation has articles, code examples etc. that let you overview the various parts of the library.

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

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:

License

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

About

MockingKit is a Swift mocking library that makes it easy to mock protocol implementations for unit tests and not yet implemented functionality. It lets you register function results, invoke method calls and inspect invokations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.0%
  • Ruby 5.0%