Cannot override a specific dependancy using a preview trait #292
-
I'm using a repository & protocol via dependencies. I'm wanting to override the dependancy to use a specific mocked version in my preview rather than the protocol FooRepositoryProtocol { ... }
final class FooRepository: FooRepositoryProtocol { ... }
private enum FooRepositoryKey: DependencyKey, TestDependencyKey {
static let liveValue: FooRepositoryProtocol = FooRepository()
static let previewValue: FooRepositoryProtocol = MockFooRepository()
}
extension DependencyValues {
var fooRepository: FooRepositoryProtocol {
get { self[FooRepositoryKey.self] }
set { self[FooRepositoryKey.self] = newValue }
}
}
@available(iOS 18, *)
#Preview(traits: .dependency(\.fooRepository, .mock)) {
NavigationStack {
SearchForFriendsView()
}
}
private extension FooRepositoryProtocol where Self == MockFooRepository {
static var mock: MockFooRepository { MockFooRepository() }
} Appreciate any thoughts! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @wtpalexander, is it possible to provide a compiling, self-contained example of the behavior you are seeing? I'm not seeing this when I try it out. Here's an example of overriding a depending in the preview: And this is what happens when I do not override the dependency: |
Beta Was this translation helpful? Give feedback.
Hi @wtpalexander, is it possible to provide a compiling, self-contained example of the behavior you are seeing? I'm not seeing this when I try it out. Here's an example of overriding a depending in the preview:
And this is what happens when I do not override the dependency: