Skip to content

Instantly share code, notes, and snippets.

@turekj
Created January 8, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turekj/a21612c0f21877967ce06a6341e8f015 to your computer and use it in GitHub Desktop.
Save turekj/a21612c0f21877967ce06a6341e8f015 to your computer and use it in GitHub Desktop.
Supplementary behaviour code for https://jakubturek.com/bottom-up-unit-testing/
class YearsBetweenDatesBehavior:
Behavior<YearsBetweenDatesBehavior.Context> {
struct Context {
let sut: (Calendar) -> (Date, Date) -> Int?
let from: String
let to: String
let calendar: Calendar.Identifier
let expected: Int
}
override class func spec(_ aContext: @escaping
() -> YearsBetweenDatesBehavior.Context) {
let calendar = aContext().calendar
let from = aContext().from
let to = aContext().to
let expected = aContext().expected
describe("years between") {
context("with calendar \(calendar)") {
var sut: ((Date, Date) -> Int?)!
beforeEach {
sut = aContext().sut(Calendar(identifier: calendar))
}
afterEach {
sut = nil
}
context("from \(from) to \(to)") {
var yearsBetween: Int?
beforeEach {
yearsBetween = sut(from.date, to.date)
}
afterEach {
yearsBetween = nil
}
it("should return \(expected) years") {
expect(yearsBetween).toNot(beNil())
expect(yearsBetween) == expected
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment