Skip to content

Instantly share code, notes, and snippets.

@Gurpartap
Last active August 29, 2015 14:14
Show Gist options
  • Save Gurpartap/b67968bd88c6e75287cc to your computer and use it in GitHub Desktop.
Save Gurpartap/b67968bd88c6e75287cc to your computer and use it in GitHub Desktop.
Quick bug? Update: Not anymore.
import Quick
import Nimble
class LibrarySpec: QuickSpec {
override func spec() {
describe("library") {
var library: Library!
beforeEach {
library = Library()
}
context("when initialized") {
it("has no playlists") {
expect(library.hasPlaylists()).to(beFalse())
}
}
context("when populated with valid playlists") {
// where can i use library initialized by beforeEach?
// a) library.addPlaylist(Playlist())
// but library is nil here. beforeEach was not called?
// fatal error: unexpectedly found nil while unwrapping an Optional value
// Fix: Wrap it in beforeEach.
beforeEach {
library.addPlaylist(Playlist())
}
it("has playlists") {
// b) library.addPlaylist(Playlist())
// works...
expect(library.hasPlaylists()).to(beTrue())
}
it("has only 1 playlist") {
// b) library.addPlaylist(Playlist())
// works...
expect(library.size()).to(equal(1))
}
}
}
}
}
struct Library {
var playlists = Array<Playlist>()
mutating func addPlaylist(playlist: Playlist) {
playlists.append(playlist)
}
func size() -> Int {
return playlists.count
}
func hasPlaylists() -> Bool {
return size() > 0
}
}
struct Playlist {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment