NimbleMockSix 0.1.4

NimbleMockSix 0.1.4

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最新发布2018年1月
SwiftSwift 版本3.1
SPM支持 SPM

Tamas Lustyik 维护。



 
依赖
MockSix= 0.1.7
Nimble~> 7.0
 

Nimble 匹配器,用于 MockSix

预告

使用 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():参数是 nil
  • any():参数匹配任何内容(总是通过)
  • 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 许可下发布。