Swiftpack.co - Package - danielsaidi/Mockery


Mockery Logo
Version Swift 5.0 License Twitter: @danielsaidi

About Mockery

Mockery is a mocking library for Swift. Mockery lets you register function results, invoke method calls and inspect invokations:

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

class MyMock: Mock, MyProtocol {

    lazy var doStuffRef = MockReference(doStuff)  // This has to be lazy

    func doStuff(int: Int, string: String) -> String {
        invoke(doStuffRef, args: (int, string))

let mock = MyMock()
mock.registerResult(for: mock.doStuffRef) { args in String(args.1.reversed()) }
let result = mock.doStuff(int: 42, string: "string")    // => "gnirts"
let inv = mock.invokations(of: mock.doStuffRef)         // => 1 item
inv[0].arguments.0                                      // => 42
inv[0].arguments.1                                      // => "message"
inv[0].result                                           // => "gnirts"
mock.hasInvoked(mock.doStuffRef)                        // => true
mock.hasInvoked(mock.doStuffRef, numberOfTimes: 1)      // => true
mock.hasInvoked(mock.doStuffRef, numberOfTimes: 2)      // => false

Mockery supports:

  • mocking protocols
  • mocking classes (using [Mockable][Mockable])
  • mocking synchronous and asynchronous functions.
  • mocking non-returning and returning functions.
  • void, optional and non-optional result values.
  • argument-based, variable result values.

Mockery doesn't put any restrains on your code or require you to structure it in any way. Just create a mock when you want to mock a protocol and you're good to go.

For more information, have a look at this detailed example.


Swift Package Manager



pod 'Mockery'

Demo App

This repository contains a demo app that shows you how to use Mockery. To run it, just open and run MockeryDemo.xcodeproj.

Contact me

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


Mockery is inspired by Stubber, and would not have been possible without it. However, Stubber uses global functions, which requires you to reset the global state every now and then. Mockery moves this logic to each mock, which means that any recorded exeuctions are automatically reset when the mock is disposed. Mockery also adds some extra functionality, like support for optional and void results and convenient inspection utilities.


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


Stars: 10



- 2020-09-15 06:47:20

This version supports macOS 10.10 instead of 10.15.

- 2020-09-05 10:22:48

This version adds improved support for watchOS, tvOS and macOS.

- 2020-08-03 07:03:59

This version adds mock classes that lets you commonly used Foundation classes:

  • MockNotificationCenter
  • MockUserDefaults

- 2020-07-17 09:35:15

This version replaces the memory address resolve logic with a new MockReference approach.

MockReference, replaces the brittle memory address-based approach from previous versions and makes invokations much more reliable. This means that it's now possible to use Mockery for more architectures and devices than 64 bit, that mocks can be shared across frameworks etc.

- 2020-06-29 06:55:25

This version removes deprecations and updates to latest Quick and Nimble.