Skip to content

Instantly share code, notes, and snippets.

@listrophy
Created January 7, 2015 00:42
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 listrophy/1886cbe1fcc4057a52e2 to your computer and use it in GitHub Desktop.
Save listrophy/1886cbe1fcc4057a52e2 to your computer and use it in GitHub Desktop.
Quick does not instantiate new objects for each test
class Mutator {
var mutated: Bool
init() {
mutated = false
}
func mutate() {
mutated = true
}
}
// Please note that this style does not work, but I'd like it to!
// Apple's built-in unit testing framework allows for this. How?
// Answer: They instatiate a new test object for each test
class MyQuickTest: QuickSpec {
override func spec() {
describe("Mutator") {
let subject = Mutator()
it("test 1") {
expect(subject.mutated).to(beFalse())
subject.mutate()
expect(subject.mutated).to(beTrue())
}
it("test 2") {
expect(subject.mutated).to(beFalse()) // currently fails because `subject` is same mutated object from "test 1"
subject.mutate()
expect(subject.mutated).to(beTrue())
}
}
}
}
// undesirables:
// 1. must declare shared variable as "var" and not "let"
// 2. must use `beforeEach` to instantiate before each test run
// 3. must use "!" (or worse `if let`) to access underlying value since it's now an optional
class MyQuickTest: QuickSpec {
override func spec() {
describe("Mutator") {
var subject: Mutator?
beforeEach {
subject = Mutator()
}
it("test 1") {
expect(subject!.mutated).to(beFalse())
subject!.mutate()
expect(subject!.mutated).to(beTrue())
}
it("test 2") {
expect(subject!.mutated).to(beFalse())
subject!.mutate()
expect(subject!.mutated).to(beTrue())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment