Improved the FileServiceMock mock in the tests target to support multiple actions.

This commit is contained in:
Javier Cicchelli 2025-01-13 00:32:16 +01:00
parent 12151deea0
commit 2852f4b1bf
2 changed files with 45 additions and 8 deletions

View File

@ -24,7 +24,7 @@ struct FileServiceTests {
#expect(folder.isFileURL == true) #expect(folder.isFileURL == true)
} }
// MARK: Functions // MARK: Functions tests
@Test(arguments: [URL.someNewFolder, .someNewFile]) @Test(arguments: [URL.someNewFolder, .someNewFile])
func createFolder(with url: URL) async throws { func createFolder(with url: URL) async throws {

View File

@ -1,13 +1,14 @@
import ColibriLibrary import ColibriLibrary
import Foundation import Foundation
struct FileServiceMock { final class FileServiceMock {
// MARK: Properties // MARK: Properties
private let action: Action?
private let folder: URL private let folder: URL
private var actions: [Action] = []
private weak var spy: FileServiceSpy? private weak var spy: FileServiceSpy?
// MARK: Initialisers // MARK: Initialisers
@ -17,7 +18,21 @@ struct FileServiceMock {
action: Action? = nil, action: Action? = nil,
spy: FileServiceSpy? = nil spy: FileServiceSpy? = nil
) { ) {
self.action = action self.actions = if let action {
[action]
} else {
[]
}
self.folder = currentFolder
self.spy = spy
}
init(
currentFolder: URL,
actions: [Action],
spy: FileServiceSpy? = nil
) {
self.actions = actions
self.folder = currentFolder self.folder = currentFolder
self.spy = spy self.spy = spy
} }
@ -37,7 +52,9 @@ extension FileServiceMock: FileServicing {
// MARK: Functions // MARK: Functions
func createFolder(at url: URL) async throws(FileServiceError) { func createFolder(at url: URL) async throws(FileServiceError) {
switch action { guard let nextAction else { return }
switch nextAction {
case .error(let error): case .error(let error):
throw error throw error
case let .createFolder(url): case let .createFolder(url):
@ -48,7 +65,9 @@ extension FileServiceMock: FileServicing {
} }
func delete(at url: URL) async throws(FileServiceError) { func delete(at url: URL) async throws(FileServiceError) {
switch action { guard let nextAction else { return }
switch nextAction {
case .error(let error): case .error(let error):
throw error throw error
case let .delete(url): case let .delete(url):
@ -59,7 +78,9 @@ extension FileServiceMock: FileServicing {
} }
func exists(at url: URL) async throws(FileServiceError) -> Bool { func exists(at url: URL) async throws(FileServiceError) -> Bool {
switch action { guard let nextAction else { return false }
switch nextAction {
case .error(let error): case .error(let error):
throw error throw error
case let .exists(url, exists): case let .exists(url, exists):
@ -72,7 +93,23 @@ extension FileServiceMock: FileServicing {
} }
// MARK: - Enumerations // MARK: - Helpers
private extension FileServiceMock {
// MARK: Computed
var nextAction: Action? {
guard !actions.isEmpty else {
return nil
}
return actions.removeFirst()
}
}
// MARK: - Actions
extension FileServiceMock { extension FileServiceMock {
enum Action { enum Action {