Moocher
A set of “rspec-like” test matchers that “mooch” off of XCTest
Adding Moocher to your project
CocoaPods
CocoaPods is the recommended way to add Utensils to your project.
- Add Moocher to your Podfile
pod 'Moocher'
. - Install the pod(s) by running
pod install
. - Add Utensils to your files with
import Moocher
.
Swift Package manager
Swift Package Manager can be used to add Moocher
the to your project:
- Add
.package(url: "https://github.com/rbaumbach/Moocher", from: "0.0.1")
- Follow intructions to add the
Moocher
package to your project.
Note: Since Moocher
requires the library XCTest
, when you add Moocher
to your project, that it’s only added to your unit testing target.
Clone from Github
- Clone repository from github and copy files directly, or add it as a git submodule.
- Add all files from
Source
directory to your project.
Usage
Moocher matchers are inspired by RSpec:
expect(AnswerToUltimateQuestion.value).to.equal(42)
Equivalence and Identity
equal
This matcher works for types that conform to Equatable
, but also works for types that conform to FloatingPoint
as well.
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 throwError
matcher can be used in 4 different ways:
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 Collection
or are String
s.
let emptyArray: [Int] = []
expect(emptyArray).to.beEmpty()
expect([1, 2, 3]).toNot.beEmpty()
expect("").to.beEmpty()
expect("Taco").to.beEmpty()