dave08
08/21/2024, 9:48 AMshared { }
defined at top level, is supposed to run once for all specs, or is recreated per spec (I mean class where there are a bunch of tests or suites)?dave08
08/21/2024, 10:20 AMbeforeSpec { }
concept either? I guess the obvious Prepared solution is declare a top level val prepareDb = shared { ... }
and then declare val db by prepareDb
in the tests... the problem is that prepareDb
can't be depended on by other top levels, leading to redeclaring the db (test container) AND the sql client in every test class... a bit more verbose, but again, this concept of writing everything in the test and not having global state?CLOVIS
08/21/2024, 1:33 PMis supposed to run once for all specsYes, if at least one test uses it. If no tests use it, it doesn't run at all
CLOVIS
08/21/2024, 1:34 PMval prepareDb = shared { ... }Please keep the naming separate, prepared & shared values are two very different things! don't confuse yourself You can invoke shared values from prepared values, but not the opposite
dave08
08/21/2024, 1:35 PMdave08
08/21/2024, 1:36 PMPlease keep the naming separate,...oops I guess that's
val shareDb = ...
?CLOVIS
08/21/2024, 1:37 PMdave08
08/21/2024, 1:39 PMAlthough SharedProvider is conceptually equivalent to PreparedProvider, and can be used to generate multiple Shared instances from the same block in exactly the same way, this is not recommended.so there's no such thing...?
dave08
08/21/2024, 1:40 PMCLOVIS
08/21/2024, 1:41 PMfun randomInt() = prepared { random.nextInt() }
val int1 by randomInt()
val int2 by randomInt()
this creates two different prepared values with the same init code (they will really have different values when ran), because the "identity" of a prepared value is the variable it is bound to
you can do the same with shared values, but IMO it's a bad ideadave08
08/21/2024, 1:42 PMCLOVIS
08/21/2024, 1:42 PMval randomInt = prepared { … }
val int1 by randomInt
val int2 by randomInt
to get multiple prepared values from the same init codedave08
08/21/2024, 1:43 PMCLOVIS
08/21/2024, 1:43 PMSo using them in separate classes is OK then?Yes. Since you always have to refer to them explicitly, you can declare them anywhere you want, even at the top level: they can never accidentally impact another test
dave08
08/21/2024, 1:44 PMSince you always have to refer to them explicitlyeven when using them multiple times in the same class you refer to each one explicitly?
CLOVIS
08/21/2024, 1:45 PMCLOVIS
08/21/2024, 1:46 PMdave08
08/21/2024, 1:52 PMval db = shared { … }
val db1 by db
val db2 by db
more than if they were used in separate classes... the only little problem is that there's no way to clean them up really... so in test container's we'd need to rely on ryuk for example...CLOVIS
08/21/2024, 1:54 PMCLOVIS
08/21/2024, 1:55 PMCLOVIS
08/21/2024, 1:56 PMdave08
08/21/2024, 2:03 PMdave08
08/21/2024, 2:06 PMCLOVIS
08/21/2024, 2:13 PMCLOVIS
08/21/2024, 2:14 PMdave08
08/21/2024, 2:15 PMdave08
08/21/2024, 2:16 PMIn those three bullet points, you'll notice that the value must be immutable and side-effect-free. If that's the case, then binding it multiple times makes no sense because they will all get the same valuethis is very practical.
CLOVIS
08/21/2024, 2:17 PMCLOVIS
08/21/2024, 2:17 PMdave08
08/21/2024, 2:18 PM