一组类似于"rspec-like"的测试匹配器,可以"借用"自XCTest
。
将Moocher添加到您的项目中
CocoaPods
CocoaPods是将Moocher
添加到您项目的推荐方法。
- 将
Moocher
添加到Podfile:pod 'Moocher'
。 - 通过运行
pod install
来安装pod(s)。 - 使用
import Moocher
将Moocher
添加到您的文件中。
Swift Package Manager
Swift Package Manager 可用于将 Moocher
添加到您的项目中。
- 添加
.package(url: "https://github.com/rbaumbach/Moocher", from: "0.4.0")
- 按照说明 添加
Moocher
包到您的项目中。
注意:由于 Moocher
需要库 XCTest
,当您向项目添加 Moocher
时,请确保它已添加到单元测试目标中。
从 GitHub 克隆
- 从 GitHub 克隆仓库并将文件直接复制,或将其作为 git 子模块添加。
- 将
Sources/Moocher
目录中的所有文件添加到您的项目中。
用法
Moocher 匹配器受到 RSpec 的启发
expect(AnswerToUltimateQuestion.value).to.equal(42)
等价和身份
equal
此匹配器适用于符合 Equatable
的类型,但同时也适用于符合 FloatingPoint
的类型。
expect(99).to.equal(99)
expect(99).toNot.equal(100)
expect(9.9).to.equal(9.9, within: 0.1)
expect(9.9).toNot.equal(10.9, within: 0.1)
类型
beInstanceOf
class Dog { }
let chihuahua = Dog()
let pancho = chihuahua
let miniPinscher = Dog()
expect(chihuahua).to.beInstanceOf(pancho)
expect(chihuahua).toNot.beInstanceOf(miniPinscher)
beKindOf
class Dog { }
struct Fish { }
let dog = Dog()
expect(dog).to.beKindOf(Dog.self)
expect(dog).toNot.beKindOf(Fish.self)
conformTo
protocol Wolf { }
class Dog: Wolf { }
protocol Goat { }
let dog = Dog()
expect(dog).to.conformTo(Wolf.self)
expect(dog).toNot.conformTo(Goat.self)
比较
beLessThan
expect(9).to.beLessThan(10)
expect(9).toNot.beLessThan(8)
beLessThanOrEqualTo
expect(9).to.beLessThanOrEqualTo(9)
expect(11).toNot.beLessThanOrEqualTo(10)
beGreaterThan
expect(9).to.beGreaterThan(8)
expect(7).toNot.beGreaterThan(8)
beGreaterThanOrEqualTo
expect(9).to.beGreaterThanOrEqualTo(8)
expect(7).toNot.beGreaterThanOrEqualTo(8)
真值
beTruthy
expect(true).to.beTruthy()
expect(false).toNot.beTruthy()
beFalsy
expect(false).to.beFalsy()
expect(true).toNot.beFalsy()
beNil
var number: Int?
expect(number).to.beNil()
number = 99
expect(number).toNot.beNil()
错误抛出
throwError
throwError
匹配器可以用以下4种方式使用
throwError(block:)
expect({ try functionThatThrowsAnError() })
.to.throwError()
expect({ try functionThatDoesNotThrowAnError() })
.toNot.throwError()
throwError(block:errorHandler:)
expect({ try functionThatThrowsAnError() })
.to.throwError { error in
// Do something with error
}
throwError(block:specificError:)
expect({ try functionThatThrowsABurritoError() })
.to.throwError(specificError: BurritoError.beansMissing))
expect({ try functionThatThrowsABurritoError() })
.toNot.throwError(specificError: TacoError.salsaMissing))
throwError(block:errorType:)
expect({ try functionThatThrowsABurritoError() })
.to.throwError(errorType: BurritoError.self))
expect({ try functionThatThrowsABurritoError() })
.toNot.throwError(errorType: TacoError.self))
集合
beEmpty
此匹配器适用于符合Collection
的类型。
let emptyArray: [Int] = []
expect(emptyArray).to.beEmpty()
expect([1, 2, 3]).toNot.beEmpty()
此匹配器也适用于String
。
expect("").to.beEmpty()
expect("Taco").toNot.beEmpty()
startWith
expect(["a", "b", "c"]).to.startWith("a")
expect([2, 4, 8]).toNot.startWith(1)
endWith
expect(["a", "b", "c"]).to.endWith("c")
expect([2, 4, 8]).toNot.endWith(7)
haveSizeOf
expect(["taco", "burrito"]).to.haveSizeOf(2)
expect([2, 4, 8]).toNot.haveSizeOf(7)
序列
包含
此匹配器适用于符合 序列
类型的情况。
expect(["uno", "dos", "tres"]).to.contain("tres")
expect("Billy Goat").toNot.contain("$")
此匹配器也可以用于找到 字符串
中的子字符串。
expect("Corey and Trevor").to.contain("and")
expect("Richard LaFleur").toNot.contain("Smart")
复合匹配器
一些匹配器允许复合断言
expect([1, 2, 3]).to.contain(1).and.startWith(1).and.endWith(3)
expect([1, 2, 3]]).toNot.haveSizeOf(4).and.contain(7)
以下匹配器可以用复合方式使用
startWith
endWith
包含
haveSizeOf
具有最小值
具有最大值
贪心轮询
此存储库还包含一个用于在必须“等待”断言的集成测试中轮询值additional library。
这可以添加到您的项目中
CocoaPods
- 将
MoocherPolling
添加到您的 Podfile 中pod 'Moocher/Polling'
。 - 通过运行
pod install
来安装pod(s)。 - 使用
import MoocherPolling
将MoocherPolling
添加到您的文件中。
Swift 包管理器
- 将
MoocherPolling
添加到您的目标:构建阶段
->链接二进制库
。 - 使用
import MoocherPolling
将MoocherPolling
添加到您的文件中。
挂起
挂起
是一种允许在指定时间段内等待断言的函数。
hangOn(for: .seconds(10)) { complete in
var number = 100
DispatchQueue.global(qos: .background).async { [weak self] in
sleep(3)
number = 99
DispatchQueue.main.async {
expect(number).to.equal(99)
complete()
}
}
}
toSomeday 和 toNever
可以使用熟悉的 expect
语法通过 toSomeday
和 toNever
持续轮询值。
var dogArray = ["Chihuhahua", "Miniature Pinscher", "Border Collie"]
DispatchQueue.global(qos: .background).async {
sleep(3)
dogArray.append("German Shepard")
}
expect(dogArray).toSomeday.contain("German Shepard")
var dogArray = ["Chihuhahua", "Miniature Pinscher", "Border Collie"]
DispatchQueue.global(qos: .background).async {
sleep(3)
dogArray.append("German Shepard")
}
expect(dogArray).toNever.contain("Poodle")
expect
子句会阻塞测试运行,直到超时或期望的断言为真。默认超时为 5
秒。可以通过提供 timeout
值来设置。
对断言值进行轮询的间隔默认为 100
毫秒。可以通过提供 pollingInterval
值来设置。
expect(dogArray, timeout: .seconds(10), pollingInterval: .miliseconds(500)).toSomeday.contain("German Shepard")
toSomeday
和 toNever
可以使用以下匹配器
beNil
equal
包含