-
At my jobby job we have recently started adopting this library for dependency management. In our class AppDelegate: UIResponder, UIApplicationDelegate {
@Dependency(\.myService) var myService
// ... cut for brevity
} When the app launches from a UI test, it is given a launch argument indicating that it should use the mock implementation. Previously, we would do something like this: class AppDelegate: UIResponder, UIApplicationDelegate {
var service: MyService!
init() {
if ProcessInfo.processInfo.arguments.contains("useMockedService") {
service = MockedMyService()
} else {
service = LiveMyService()
}
}
} Is it possible to achieve the same result when using I realize that it might be weird to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Since you are using the dependency at the very root level entry point of the application, I think the only thing you can do is check for the environment variable inside the extension MyServiceKey: DependencyKey {
static var liveValue: any MyService {
if ProcessInfo.processInfo.arguments.contains("useMockedService") {
return MockedMyService()
} else {
return LiveMyService()
}
}
} If you did not use |
Beta Was this translation helpful? Give feedback.
Since you are using the dependency at the very root level entry point of the application, I think the only thing you can do is check for the environment variable inside the
liveValue
in order to provide a mock implementation:If you did not use
@Dependency
at such a root level you would have more options. For example, in the Standups app we built, the entry point of the app creates the rootAppView
, which uses@Dependency
. That gives us enough wiggle room …