一组测试匹配器,从 XCTest 中嘶吼

乞丐比特里斯 椰子荚版本 SPM 兼容 椰荚平台 许可证

一组“类似 rspec”的测试匹配器,它们从 XCTest 中“嘶嘶”出来

将穆赫添加到您的项目中

可可豆荚

CocoaPods是将餐具添加到项目中的推荐方法。

  1. 将 Moocher 添加到您的 Podfile 中。pod 'Moocher'
  2. 通过运行 来安装 Pod。pod install
  3. 使用 将器具添加到文件中。import Moocher

迅捷包管理器

Swift Package Manager 可用于将 添加到您的项目中:Moocher

  1. .package(url: "https://github.com/rbaumbach/Moocher", from: "0.0.1")
  2. 按照入侵将包添加到项目中。Moocher

注意:由于需要库,当您添加到项目中时,它只会添加到单元测试目标中。MoocherXCTestMoocher

从 Github 克隆

  1. 从 github 克隆存储库并直接复制文件,或将其添加为 git 子模块。
  2. 将目录中的所有文件添加到项目中。Source

用法

穆赫匹配器的灵感来自RSpec

expect(AnswerToUltimateQuestion.value).to.equal(42)

等效性和同一性

  • equal

此匹配器适用于符合 的类型,也适用于符合 的类型。EquatableFloatingPoint

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)

Types

  • 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)

Comparisons

  • 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)

Truthiness

  • 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()

Error Throwing

  • throwError

The matcher can be used in 4 different ways:throwError

  • 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))

Collection

  • beEmpty

This matcher works for types that conform to or are s.CollectionString

let emptyArray: [Int] = []

expect(emptyArray).to.beEmpty()
expect([1, 2, 3]).toNot.beEmpty()

expect("").to.beEmpty()
expect("Taco").to.beEmpty()

GitHub

点击跳转