使用 NimbleMockSix,您可以轻松地对 MockSix 模拟对象上的方法调用进行断言。假设您已经设置了以下模拟对象(有关详细信息,请参阅 MockSix 文档)
// 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: [])
}
}
测试方法的调用次数
// 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
测试调用的参数
// 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
还有更多功能!
当前已实现的匹配器
receive(_:times:)
、receive(_:atLeastTimes:)
、receive(_:atMostTimes:)
receive(_:times:with:)
、receive(_:atLeastTimes:with:)
、receive(_:atMostTimes:with:)
当前已实现的参数验证器
theValue(_:)
:参数匹配给定的值nilValue()
:参数为 nilany()
:参数匹配任何值(始终通过)any(of:)
:参数匹配数组中的任何一个值any(passing:)
:参数使谓词为真构建:Swift 4.2
使用:macOS 10.10+、iOS 8.4+、tvOS 9.2+、Linux
通过 Cocoapods:将以下行添加到您的 Podfile
pod 'NimbleMockSix'
通过 Carthage:将以下行添加到您的 Cartfile(或 Cartfile.private)
github "lvsti/NimbleMockSix"
github "Quick/Quick"
通过 Swift Package Manager:将其添加到您的 Package.swift 中的 dependencies 中
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 ...
]
)
NimbleMockSix 在 MIT 许可证下发布。