NimbleMockSix
Teaser
With NimbleMockSix, you can easily make expectations on method invocations on MockSix mock objects. Suppose you have the following mock already in place (for details, see the MockSix documentation):
// original interface to mock
protocol MyClassProtocol {
func myFunc(string: String, number: Double) -> [Int]
}
// original implementation
class MyClass: MyClassProtocol {
func myFunc(string: String, number: Double) -> [Int] {
// ... whatever ...
return [1, 2, 3]
}
}
// mock implementation
class MockMyClass: MyClassProtocol, Mock {
enum Methods: Int {
case myFunc
}
typealias MockMethod = Methods
func myFunc(string: String, number: Double) -> [Int] {
return registerInvocation(for: .myFunc,
args: string, number
andReturn: [])
}
}
To test for the invocation count of a method:
// given
let myMock = MockMyClass()
myMock.stub(.myFunc, andReturn: [42])
// when
myMock.myFunc(string: "aaa", number: 3.14)
myMock.myFunc(string: "bbb", number: 6.28)
// then
expect(myMock).to(receive(.myFunc, times: 2)) // --> passes
To test the arguments of an invocation:
// given
let myMock = MockMyClass()
myMock.stub(.myFunc, andReturn: [42])
// when
myMock.myFunc(string: "aaa", number: 3.14)
expect(myMock).to(receive(.myFunc, with: [
theValue("aaa"),
any()
])) // --> passes
expect(myMock).to(receive(.myFunc, with: [
any(of: ["bbb", "ccc"]),
any { x in x >= 3.0 && x < 4.0 }
])) // --> fails
But there is more!
Currently implemented matchers:
- just invocation count constraints:
receive(_:times:)
,receive(_:atLeastTimes:)
,receive(_:atMostTimes:)
- invocation count AND argument constraints:
receive(_:times:with:)
,receive(_:atLeastTimes:with:)
,receive(_:atMostTimes:with:)
:
Currently implemented argument verifiers:
theValue(_:)
: argument matches the given valuenilValue()
: argument is nilany()
: argument matches anything (always passes)any(of:)
: argument matches any of the values in the arrayany(passing:)
: argument makes the predicate true
Requirements
To build: Swift 4.2
To use: macOS 10.10+, iOS 8.4+, tvOS 9.2+, Linux
Installation
Via Cocoapods: add the following line to your Podfile:
pod 'NimbleMockSix'
Via Carthage: add the following lines to your Cartfile (or Cartfile.private):
github "lvsti/NimbleMockSix"
github "Quick/Quick"
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/NimbleMockSix", from: "0.1.3"),
.package(url: "https://github.com/Quick/Quick", from: "1.2.0"),
// ... other dependencies ...
]
)
License
NimbleMockSix is released under the MIT license.