Fake test double: a simplified collaborator

A fake object is a simplified version of a collaborator, with similar or partial functionalities, but implemented in a simpler way. Considering a version of TaskManager that stores the tasks locally, using a store object as a collaborator, whose implementation is unknown to the TaskManager, it might be implemented as file-persistent on file or database-persistent with CoreData.

TaskManager relies on the store to persist the tasks:

class TaskManager {
private let store: TaskStore

init(store: TaskStore) {
self.store = store
}
func add(task: Task) {
store.add(task: task)
}
var count: Int {
return store.allTasks.count
}
}

TaskStore has the following interface:

protocol TaskStore {
var allTasks: [Task] { get }
func add(task: Task)
}

The fake implementation of the store, instead of using CoreData, will use a simple array, which we can write as follows:

class FakeTaskStore: TaskStore {
private(set) var allTasks = [Task]()

func add(task: Task) {
allTasks.append(task)
}
}

Similarly to the previous one, the test is simply as follows:

func testNumberOfTasks() {
let taskManager = TaskManager(store: FakeTaskStore())
taskManager.add(task: Task())
taskManager.add(task: Task())
XCTAssertEqual(taskManager.count, 2)
}