In a previous post, I showed how to set up a test in ZIO-land. This is more a how it works post.
If you recall, we created a ZIO that needs layers to unit test it. To expand, we have a ZIO that looks like this:
ZIO[Init with Flow with Errors, Throwable, ProblemList]
where Init, Flow and Errors are my bespoke layers and ProblemList is just a type alise for a List of Eithers.
We add these layers later with provideLayer with implementations that are for testing or production. The interesting code has already been called, that is the construction of the ZIO in the first place. For me, it looks like:
def flow(paths: Filenames,
configFile: String,
session: SparkSession,
fs: FileSystem): ZIO[Init with Flow with Errors, Throwable, ProblemList] = for {
s <- Init.init(configFile, session, fs)
results <- Flow.resultsFor(s, paths, session, fs)
...
Now, taking the first element in the for comprehension, we see it looks like:
def init(configFile: String,
session: SparkSession,
fs: FileSystem): ZIO[Init, Throwable, Settings]
= ZIO.accessM(_.get.initializeWith(configFile, session, fs))
The take away point of all this is that you can defer adding a dependency until after the for-comprehension code (but before you execute it). This is very convenient for testing.
No comments:
Post a Comment