Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
danielsaidi/MockingKit
MockingKit
About MockingKit
MockingKit is a Swift-based mocking library that makes it easy to mock protocols and classes. It lets you register
function results, call
functions and inspect
calls:
protocol MyProtocol {
func doStuff(int: Int, string: String) -> String
}
class MyMock: Mock, MyProtocol {
lazy var doStuffRef = MockReference(doStuff) // References must be lazy
func doStuff(int: Int, string: String) -> String {
call(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 calls = mock.calls(to: mock.doStuffRef) // => 1 item
calls[0].arguments.0 // => 42
calls[0].arguments.1 // => "string"
calls[0].result // => "gnirts"
mock.hasCalled(mock.doStuffRef) // => true
mock.hasCalled(mock.doStuffRef, numberOfTimes: 1) // => true
mock.hasCalled(mock.doStuffRef, numberOfTimes: 2) // => false
MockingKit supports:
- mocking protocols
- mocking classes
- mocking non-returning and returning functions
- mocking synchronous and asynchronous functions
void
,optional
andnon-optional
result values- argument-based results
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.
Installation
Swift Package Manager
https://github.com/danielsaidi/MockingKit.git
CocoaPods
pod 'MockingKit'
Demo App
This repository contains a demo app that shows you how to use MockingKit.
To run it, just open and run Demo/Demo.xcodeproj
.
Contact me
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
Acknowledgements
MockingKit is based on Stubber, and would not have been possible without it.
License
MockingKit is available under the MIT license. See the LICENSE file for more info.