Skip to content

Dependencies

maximkrouk edited this page May 5, 2022 · 1 revision

Dependencies

This type acts as a namespace to reference your dependencies.

public struct Dependencies 

To declare a dependency, create a DependencyKey, and declare a computed property in this type like you would declare a custom EnvironmentValue in SwiftUI. For example, if UUIDGeneratorKey is a DependencyKey with DependencyKey/Value == () -> UUID:

extension Dependencies {
  var uuidGenerator: () -> UUID {
    get { self[UUIDGeneratorKey.self] }
    set { self[UUIDGeneratorKey.self] = newValue }
  }
}

This dependency can then be referenced by its keypath \.uuidGenerator when invoking the Dependency property wrapper.

Methods

clearAliases()

Use this static method to reset all aliases you may have set between dependencies. You typically call this method during the setUp() method of some XCTestCase subclass:​

public static func clearAliases() 
class SomeFeatureTests: XCTextCase {
  override func setUp() {
    super.setUp()
    Dependencies.clearAliases()
  }
  // …
}

clearAliases()

Use this static method to reset all aliases you may have set between dependencies. You typically call this method during the setUp() method of some XCTestCase subclass:​

public static func clearAliases() 
class SomeFeatureTests: XCTextCase {
  override func setUp() {
    super.setUp()
    Dependencies.clearAliases()
  }
  // …
}

reset()

Use this static method to reset all global depedencies to their default values. You typically call this method during the setUp() method of some XCTestCase subclass:​

public static func reset() 
class SomeFeatureTests: XCTextCase {
  override func setUp() {
    super.setUp()
    Dependencies.reset()
  }
  // …
}