Created
January 7, 2015 00:42
-
-
Save listrophy/1886cbe1fcc4057a52e2 to your computer and use it in GitHub Desktop.
Quick does not instantiate new objects for each test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mutator { | |
var mutated: Bool | |
init() { | |
mutated = false | |
} | |
func mutate() { | |
mutated = true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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