MockSix
MockSix is a microframework to make object mocking in Swift somewhat easier. MockSix is built upon Daniel Burbank's MockFive.
If you are using Quick+Nimble, make sure you check out the Nimble matcher extensions at NimbleMockSix as well.
Elevator pitch
MockSix simplifies manual object mocking by taking over some of the boilerplate and offering an API that is hard to use incorrectly.
Sample code:
// original interface
protocol MyClassProtocol {
func myFunc(_ string: String) -> [Int]
}
// actual implementation
class MyClass: MyClassProtocol {
func myFunc(_ string: String) -> [Int] {
// ... whatever ...
return result
}
}
// mock implementation
class MockMyClass: MyClassProtocol, Mock {
enum Methods: Int {
case myFunc
}
typealias MockMethod = Methods
func myFunc(_ string: String) -> [Int] {
return registerInvocation(for: .myFunc,
args: string,
andReturn: [])
}
init() {}
}
// in the test case
let mock = MockMyClass()
mock.myFunc("foobar") == [] // true
mock.stub(.myFunc, andReturn: [42])
mock.myFunc("foobar") == [42] // true
mock.stub(.myFunc) { $0.isEmpty ? [] : [42] }
mock.myFunc("foobar") == [42] // true
Requirements
To build: Swift 4
To use: macOS 10.10+, iOS 8.4+, tvOS 9.2+, Linux
Installation
Via Cocoapods: add the following line to your Podfile:
pod 'MockSix'
Via Carthage: add the following line to your Cartfile (or Cartfile.private):
github "lvsti/MockSix"
Via the Swift Package Manager: add it to the dependencies in your Package.swift:
// swift-tools-version:4.0
let package = Package(
name: "MyAwesomeApp",
dependencies: [
.package(url: "https://github.com/lvsti/MockSix", from: "0.1.7"),
// ... other dependencies ...
],
targets: [
.target(name: "MyAwesomeApp", dependencies: []),
.testTarget(
name: "MyAwesomeAppTests",
dependencies: ["MyAwesomeApp", "MockSix"]
)
],
)
Or just add MockSix.swift
and MockSixInternal.swift
to your test target.
Other stuff
I also wrote two blogposts about MockSix which may help you get started:
- Lightweight Object Mocking in Swift: motivation and design decisions, and also an overview of the MockSix Nimble matchers
- MockSix + Sourcery: Happily Ever After?: how to use Sourcery to automatically generate MockSix-style mocks without writing a single line of boilerplate
License
MockSix is released under the MIT license.