Package -

MockSix

Carthage compatible Swift Package Manager compatible CocoaPods compatible Swift 3.1 platforms

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

Requirements

To build: Swift 3.1
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:

let package = Package(
    name: "MyAwesomeApp",
    dependencies: [
        .Package(url: "https://github.com/lvsti/MockSix", majorVersion: 0),
        // ... other dependencies ...
    ]
)

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:

License

MockSix is released under the MIT license.

Github

link
Stars:

Advertisement