TestDependencies in Release Code #1559
-
We currently wrap all our client testValues in #if DEBUG statements. struct Client {}
#if DEBUG
extension Client {
static func testValue() -> Self
}
#endif This is causing a problem when implementing TestDependencyKeys and building for production. The build fails because it has to build the TestDependencyKey conformance, but cannot find out testValue. So, we have discussed a couple of options.
extension Client: TestDependencyKey {
public static var testValue: Self {
#if DEBUG
return testValue()
#else
// not sure yet (fatalError / live / noop)
#endif
}
} but this does not feel much safer Finally, I wrapped public protocol TestDependencyKey {
associatedtype Value = Self
#if DEBUG
static var previewValue: Value { get }
static var testValue: Value { get }
#endif
} TCA still builds and all tests still pass. The only issue I can see is if someone runs tests in something other than DEBUG. Any thoughts? Am I missing something? I opened a PR here #1560 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
@natemann While we used to wrap our test clients with Do you have any concerns with removing the |
Beta Was this translation helpful? Give feedback.
-
@stephencelis My concern is mostly due to having a large code base with multiple devs working in it. Also, for a more 'real world' example, take something like a databaseClient. We have a databaseClient that fetches / saves many different types of objects. Each of of these objects has a mock value that is wrapped in #If DEBUG, and these mocks are referenced in the testValue of the databaseClient. We would have to remove quite a bit of #if DEBUG statements to simply get a testValue building in release. I figured wrapping previewValue and testValue in DEBUG at the declaration probably has ramifications that you or I could not know yet. If you do not want to go down that route, we will probably go with the default values. |
Beta Was this translation helpful? Give feedback.
@natemann While we used to wrap our test clients with
#if DEBUG
we don't really think this is necessary. When SwiftUI first came out it similarly generated its Xcode preview code inside#if DEBUG
blocks, but no longer does these days.Do you have any concerns with removing the
#if DEBUG
here and allowing release builds to see the test and preview values?