Improved the FileServiceMock mock in the tests target to support multiple actions.
This commit is contained in:
parent
12151deea0
commit
2852f4b1bf
@ -24,7 +24,7 @@ struct FileServiceTests {
|
||||
#expect(folder.isFileURL == true)
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||
func createFolder(with url: URL) async throws {
|
||||
|
@ -1,13 +1,14 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
struct FileServiceMock {
|
||||
final class FileServiceMock {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let action: Action?
|
||||
private let folder: URL
|
||||
|
||||
private var actions: [Action] = []
|
||||
|
||||
private weak var spy: FileServiceSpy?
|
||||
|
||||
// MARK: Initialisers
|
||||
@ -17,7 +18,21 @@ struct FileServiceMock {
|
||||
action: Action? = 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.spy = spy
|
||||
}
|
||||
@ -37,7 +52,9 @@ extension FileServiceMock: FileServicing {
|
||||
// MARK: Functions
|
||||
|
||||
func createFolder(at url: URL) async throws(FileServiceError) {
|
||||
switch action {
|
||||
guard let nextAction else { return }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .createFolder(url):
|
||||
@ -48,7 +65,9 @@ extension FileServiceMock: FileServicing {
|
||||
}
|
||||
|
||||
func delete(at url: URL) async throws(FileServiceError) {
|
||||
switch action {
|
||||
guard let nextAction else { return }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .delete(url):
|
||||
@ -59,7 +78,9 @@ extension FileServiceMock: FileServicing {
|
||||
}
|
||||
|
||||
func exists(at url: URL) async throws(FileServiceError) -> Bool {
|
||||
switch action {
|
||||
guard let nextAction else { return false }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
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 {
|
||||
enum Action {
|
||||
|
Loading…
x
Reference in New Issue
Block a user