https://kotlinlang.org logo
#kotest
Title
# kotest
m

Moritz Post

03/03/2023, 8:36 AM
hello. I am facing a "possibly" simple issue. How would i initialise the class under test before each test run in a ShouldSpec test? I have a test like the one below but i do not know how to correctly initialize the
engine
. I don't want to change the isolation mode as i would like to do some other data loading once upfront (to feed the engine).
Copy code
class EngineTest : ShouldSpec({

    val engineInput = loadExpensiveInput()

    // does not work and lateinit is not available
    var engine: Engine
    
    beforeEach {
        engine = Engine(engineInput)
    }

    should("be ready") {
        engine.ready shouldBe true
    }
}
solved 1
d

Davio

03/03/2023, 9:08 AM
This way is how it is usually done if you want to have a new engine for each test
m

Moritz Post

03/03/2023, 9:39 AM
@Davio Which way would that be?
d

Davio

03/03/2023, 9:52 AM
Well, what is your goal? If your goals is to initialize the engine for each test, then putting it inside beforeEach is usually how it's done
m

Moritz Post

03/03/2023, 9:53 AM
where would i store the variable to reference in a test?
i don't want to make it nullable
d

Davio

03/03/2023, 9:54 AM
You can use lateinit here for Engine
m

Moritz Post

03/03/2023, 9:55 AM
unfortunately not because lateinit is only available for class properties not for regular vars (as is the case in the block here).
d

Davio

03/03/2023, 9:57 AM
Eeuhm, it works in my code?
m

Moritz Post

03/03/2023, 9:59 AM
Ok, you are right. it was an error on my side 🫣
This specifies that it does also work for local variables
m

Moritz Post

03/03/2023, 9:59 AM
indeed. thanks for bringing that up again 👍
5 Views