MockingKit is a Swift mocking library that makes it easy to mock protocol implementations and classes. It lets you invoke
method calls, inspect
invokations and register
function results:
protocol MyProtocol {
func doStuff(int: Int, string: String) -> String
}
class MyMock: Mock, MyProtocol {
lazy var doStuffRef = MockReference(doStuff) // Must 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
MockingKit supports:
- mocking protocols
- mocking classes
- mocking synchronous and asynchronous functions
- mocking non-returning and returning functions
void
,optional
andnon-optional
result values- argument-based, variable result values
MockingKit doesn't put any restrains 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.
For more information, have a look at this detailed example.
https://github.com/danielsaidi/MockingKit.git
pod 'MockingKit'
This repository contains a demo app that shows you how to use MockingKit.
To run it, just open and run Demo/Demo.xcodeproj
.
Feel free to reach out if you have questions or if you want to contribute in any way:
- E-mail: daniel.saidi@gmail.com
- Twitter: @danielsaidi
- Web site: danielsaidi.com
MockingKit is inspired by Stubber, and would not have been possible without it.
However, while Stubber uses global state, MockingKit moves this state to each mock. This means that recorded exeuctions are automatically reset when a mock is disposed.
MockingKit also adds some extra functionality, like support for optional and void results and convenient inspection utilities.
MockingKit is available under the MIT license. See the LICENSE file for more info.