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

Matt Watson

02/17/2022, 10:13 AM
Hello has anyone had this with kotest where if you need to declare private functions in a test you must do so before your tests? Seems a bit odd. Is it just the case of wacking that functionality in an 
init {}
 block?
c

christophsturm

02/17/2022, 10:20 AM
you can also declare them at the bottom outside the init block.
t

thanksforallthefish

02/17/2022, 10:21 AM
what do you mean?
Copy code
class Test : StringSpec() {
  init {
      "t" {
        test()
      }
  }

  private fun test() = ""
}
there is also this version
Copy code
class Test : StringSpec({
  fun test() = ""
  
  "t" {
    test()
  }
})
but this one is a local function, https://kotlinlang.org/docs/functions.html#local-functions, as in the second example code is inside the constructor
🙏 1
c

christophsturm

02/17/2022, 10:23 AM
if you just want to put some tests into a function for reuse you can just use the extract function refactoring and IDEA will do the right thing
🙏 1
c

Charles Flynn

02/17/2022, 10:29 AM
@Matt Watson was asking this on my behalf. I was originally using the contructor syntax which meant as local functions they had to be defined first which isn’t desirable. (Public first, private second - in this case tests first, helpers second). I’ve gone with the
init
approach. I was originally avoiding this syntax as it seemed less clean than the constructor approach and has the effect of indenting everything. But it’s preferable to having to define functions upfront.
4 Views