测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最新发布 | 2018年1月 |
SwiftSwift 版本 | 3.1 |
SPM支持 SPM | ✓ |
由 Tamas Lustyik 维护。
依赖 | |
MockSix | = 0.1.7 |
Nimble | ~> 7.0 |
使用 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 3.1
使用:macOS 10.10+、iOS 8.4+、tvOS 9.2+、Linux
通过 Cocoapods:将以下行添加到您的 Podfile 中
pod 'NimbleMockSix'
通过 Swift 打包管理器:将其添加到您的 Package.swift 的依赖关系中
let package = Package(
name: "MyAwesomeApp",
dependencies: [
.Package(url: "https://github.com/lvsti/NimbleMockSix", majorVersion: 0),
.Package(url: "https://github.com/Quick/Quick", majorVersion: 1),
// ... other dependencies ...
]
)
NimbleMockSix 在 MIT 许可下发布。