How to mock @Dependency(MyDependency.self)
#175
Answered
by
stephencelis
shimastripe
asked this question in
Q&A
-
extension APIClient: DependencyKey {
var liveValue: Self { … }
}
-extension DependencyValues {
- var apiClient: APIClient {
- get { self[APIClient.self] }
- set { self[APIClient.self] = newValue }
- }
-}
-
class MyModel {
- @Dependency(\.apiClient) var apiClient
+ @Dependency(APIClient.self) var apiClient since v1.2.0, @Environment supports a shorthand for objects. withDependencies {
$0.apiClient.get = { _ in } // cannot access.
} operation: {
MyModel()
} |
Beta Was this translation helpful? Give feedback.
Answered by
stephencelis
Jan 23, 2024
Replies: 1 comment 1 reply
-
@shimastripe We should add docs here, but you can use the subscript: withDependencies {
- $0.apiClient.get = { _ in }
+ $0[APIClient.self].get = { _ in }
} operation: {
MyModel()
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
shimastripe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shimastripe We should add docs here, but you can use the subscript: