Skip to content

Commit ddc89e7

Browse files
✅[SparkDI][Dependency Graph][WIP]- add xctestcase tests
1 parent 543dc66 commit ddc89e7

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Sources/SparkDI/DependencyContainer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public enum Scope {
1414
public enum DependencyError: Error {
1515

1616
case circularDependency
17+
18+
case missingDependency
1719
}
1820

1921
public actor DependencyContainer {

Tests/SparkDITests/DependencyGraphTests.swift

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ServiceB {
2323

2424
#if canImport(Testing)
2525

26-
struct DependencyGraphTests {
26+
struct DependencyGraphTesting {
2727

2828
@Test func circularDependency() async throws {
2929
let dependencyContainer = DependencyContainer()
@@ -72,3 +72,50 @@ struct DependencyGraphTests {
7272

7373
}
7474
#endif
75+
class DependencyGraphTests: XCTestCase {
76+
func testCircularDependency() async throws {
77+
let dependencyContainer = DependencyContainer()
78+
do {
79+
try await dependencyContainer.register(
80+
type: ServiceA.self,
81+
factory: { args in
82+
guard let serviceB = args.first as? ServiceB else { fatalError("Invalid arguments") }
83+
84+
return ServiceA(serviceB: serviceB)
85+
},
86+
argumentsTypes: [ServiceB.self],
87+
scope: .transient
88+
)
89+
90+
try await dependencyContainer.register(
91+
type: ServiceB.self,
92+
factory: { args in
93+
guard let serviceA = args.first as? ServiceA else { fatalError("Invalid arguments") }
94+
95+
return ServiceB(serviceA: serviceA)
96+
},
97+
argumentsTypes: [ServiceA.self],
98+
scope: .transient
99+
)
100+
101+
XCTFail("Should have thrown circular dependency error")
102+
103+
} catch let error as DependencyError {
104+
105+
switch error {
106+
107+
case .circularDependency:
108+
109+
break
110+
111+
default:
112+
113+
XCTFail("Wrong error type: expected circular dependency")
114+
}
115+
} catch {
116+
117+
XCTFail("Unexpected error: \(error)")
118+
}
119+
}
120+
121+
}

0 commit comments

Comments
 (0)