Implemented the MockTodoRepository repository in the unit tests target.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
@testable import class Todos.Todo
|
||||
@testable import protocol Todos.TodoRepository
|
||||
|
||||
/// A mock implementation of the TodoRepository protocol for unit testing.
|
||||
///
|
||||
/// This mock repository provides a simple in-memory storage mechanism for testing
|
||||
/// without requiring a full SwiftData ModelContext setup. It tracks inserted and
|
||||
/// deleted todos, allowing tests to verify repository operations.
|
||||
final class MockTodoRepository {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// Array of todo tasks managed by this mock repository.
|
||||
///
|
||||
/// This property tracks all todos in the repository, maintaining state across
|
||||
/// insert and delete operations. It serves as the in-memory storage for the mock,
|
||||
/// allowing tests to verify repository behavior without requiring SwiftData.
|
||||
private(set) var todos: [Todo]
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Creates a new mock repository with an optional initial set of todo tasks.
|
||||
///
|
||||
/// This initializer allows tests to set up a repository with pre-existing todos
|
||||
/// or start with an empty repository. The default parameter value provides
|
||||
/// convenient initialization for most test cases.
|
||||
///
|
||||
/// - Parameter todos: An array of todo tasks to initialize the repository with. Defaults to an empty array.
|
||||
init(todos: [Todo] = []) {
|
||||
self.todos = todos
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TodoRepository
|
||||
|
||||
extension MockTodoRepository: TodoRepository {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func delete(_ todo: Todo) {
|
||||
todos.removeAll { $0.id == todo.id }
|
||||
}
|
||||
|
||||
func insert(_ todo: Todo) {
|
||||
todos.append(todo)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user