Skip to content

Commit

Permalink
Add test for MatchesViewModel's alert upon fetch failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Richter committed Aug 8, 2015
1 parent 6b0e5f1 commit ff0a132
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SwiftGoal/ViewModels/MatchesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MatchesViewModel {
public let title: String
public let contentChangesSignal: Signal<Changeset, NoError>
public let isLoading: MutableProperty<Bool>
let alertMessageSignal: Signal<String, NoError>
public let alertMessageSignal: Signal<String, NoError>

// Actions
public lazy var deleteAction: Action<NSIndexPath, Bool, NSError> = { [unowned self] in
Expand Down
9 changes: 7 additions & 2 deletions SwiftGoalTests/MockStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftGoal

class MockStore: Store {
let players: [Player]
let matches: [Match]
var matches: [Match]? // nil is used to cause error

var didFetchMatches = false
var deletedMatch: Match?
Expand Down Expand Up @@ -46,7 +46,12 @@ class MockStore: Store {

override func fetchMatches() -> SignalProducer<[Match], NSError> {
didFetchMatches = true
return SignalProducer(value: matches)
if let matches = self.matches {
return SignalProducer(value: matches)
} else {
let error = NSError(domain: "", code: 0, userInfo: nil)
return SignalProducer(error: error)
}
}

override func deleteMatch(match: Match) -> SignalProducer<Bool, NSError> {
Expand Down
15 changes: 14 additions & 1 deletion SwiftGoalTests/ViewModels/MatchesViewModelSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ class MatchesViewModelSpec: QuickSpec {
}
}

it("raises an alert when matches cannot be fetched") {
mockStore.matches = nil // will cause fetch error

var didRaiseAlert = false
matchesViewModel.alertMessageSignal.observe(next: { alertMessage in
didRaiseAlert = true
})

matchesViewModel.active.put(true)

expect(didRaiseAlert).to(beTrue())
}

it("deletes the correct match when asked to") {
let match = mockStore.matches[1]
let match = mockStore.matches![1]
let indexPath = NSIndexPath(forRow: 1, inSection: 0)

var deletedSuccessfully = false
Expand Down

0 comments on commit ff0a132

Please sign in to comment.