Implemented the "actions" property for the FileServiceSpy spy in the tests target to support tracking multiple actions.

This commit is contained in:
Javier Cicchelli 2025-01-12 23:28:20 +01:00
parent a1ad391baa
commit 12151deea0
2 changed files with 35 additions and 27 deletions

View File

@ -39,8 +39,11 @@ struct FileServiceTests {
try await service.createFolder(at: url)
// THEN
#expect(spy.isCreateFolderCalled == true)
#expect(spy.urlCalled == url)
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .folderCreated(url))
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
@ -62,8 +65,7 @@ struct FileServiceTests {
try await service.createFolder(at: url)
}
#expect(spy.isCreateFolderCalled == false)
#expect(spy.urlCalled == nil)
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
@ -79,8 +81,11 @@ struct FileServiceTests {
try await service.delete(at: url)
// THEN
#expect(spy.isDeleteCalled == true)
#expect(spy.urlCalled == url)
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .itemDeleted(url))
}
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
@ -102,8 +107,7 @@ struct FileServiceTests {
try await service.delete(at: url)
}
#expect(spy.isDeleteCalled == false)
#expect(spy.urlCalled == nil)
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
@ -125,8 +129,9 @@ struct FileServiceTests {
// THEN
#expect(result == outcome)
#expect(spy.isExistsAtCalled == true)
#expect(spy.urlCalled == url)
let action = try #require(spy.actions.last)
#expect(action == .itemExists(url))
}
@Test(arguments: zip([URL.someRandomURL], [FileServiceError.urlNotFileURL]))
@ -147,8 +152,7 @@ struct FileServiceTests {
try await service.exists(at: url)
}
#expect(spy.isExistsAtCalled == false)
#expect(spy.urlCalled == nil)
#expect(spy.actions.isEmpty == true)
}
}

View File

@ -6,10 +6,7 @@ final class FileServiceSpy {
// MARK: Properties
private(set) var isCreateFolderCalled: Bool = false
private(set) var isDeleteCalled: Bool = false
private(set) var isExistsAtCalled: Bool = false
private(set) var urlCalled: URL?
private(set) var actions: [Action] = []
}
@ -21,21 +18,28 @@ extension FileServiceSpy: FileServicing {
}
func createFolder(at url: URL) async throws (FileServiceError) {
isCreateFolderCalled = true
urlCalled = url
actions.append(.folderCreated(url))
}
func delete(at url: URL) async throws (FileServiceError) {
isDeleteCalled = true
urlCalled = url
actions.append(.itemDeleted(url))
}
@discardableResult
func exists(at url: URL) async throws (FileServiceError) -> Bool {
isExistsAtCalled = true
urlCalled = url
actions.append(.itemExists(url))
return .random()
}
}
// MARK: - Action
extension FileServiceSpy {
enum Action: Equatable {
case folderCreated(_ url: URL)
case itemDeleted(_ url: URL)
case itemExists(_ url: URL)
}
}