Skip to content

Instantly share code, notes, and snippets.

@ncreated
Created August 20, 2017 21:27
Show Gist options
  • Save ncreated/2e968ab1728bac687962c11dfbded958 to your computer and use it in GitHub Desktop.
Save ncreated/2e968ab1728bac687962c11dfbded958 to your computer and use it in GitHub Desktop.
Medium blogpost snippet
class LevelSelectionViewModelTests: XCTestCase {
private var viewModel: LevelSelectionViewModel!
private var mockDelegate: MockLevelSelectionViewModelDelegate!
override func setUp() {
super.setUp()
let levels = [
Level(status: .unlocked, identifier: "abc"),
Level(status: .locked, identifier: "def")
]
viewModel = LevelSelectionViewModel(levels: levels)
mockDelegate = MockLevelSelectionViewModelDelegate()
viewModel.delegate = mockDelegate
}
override func tearDown() {
super.tearDown()
viewModel = nil
mockDelegate = nil
}
func test_whenSelectingUnlockedLevel_itNotifiesDelegate() {
let expectation = self.expectation(description: "delegate is called")
mockDelegate.didCall_didRequestOpeningLevelWithIdentifier = { identifier in
XCTAssertEqual(identifier, "abc")
expectation.fulfill()
}
viewModel.selectLevel(atIndex: 0) // simulate user input
waitForExpectations(timeout: 0.5, handler: nil)
}
func test_whenSelectingLockedLevel_itDoesNotNotifyDelegate() {
let expectation = self.expectation(description: "delegate is not called")
expectation.isInverted = true
mockDelegate.didCall_didRequestOpeningLevelWithIdentifier = { identifier in
expectation.fulfill()
}
viewModel.selectLevel(atIndex: 1) // simulate user input
waitForExpectations(timeout: 0.5, handler: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment