We currently keep a collection of test doubles in ...
# kotest
j
We currently keep a collection of test doubles in an object and static import them in our StringSpecs. With this, there's a test listener that "resets" all of the doubles in a BeforeTestListener. Rather than reset, I would like to make a new instance of the collection of test doubles available with each test. Currently:
Copy code
"should foo" {
  TestApplication().execute {
    someFake.bar() shouldBe false
    someOtherFake.baz() shouldBe true
  }
}
Where
TestApplication
contains the fakes and
suspend fun execute(f: suspend TestApplication.() -> Unit) = f()
, so that the body has access to any fake it needs. I'd like to get to
Copy code
"should foo" {
  someFake.bar() shouldBe false
  someOtherFake.baz() shouldBe true
}
But I'm finding it very difficult to extend or configure the test to permit this. Is this kind of thing supported/possible?
l
Seems to me you want some kind of Extension to execute your tests inside a specific 'context'
So you'd have a
beforeSpec
and
afterSpec
for example
I think you'd still need to get your fakes from the extension, so it would be
extension.someFake.bar()
, but it's close enough IMO
🙌 1
j
Wasn't sure how to access
extension
, but it lead me to this solution.
Copy code
private val apps = ConcurrentHashMap<Descriptor.TestDescriptor, TestApplication>()

    /** Enables `app` in any test to create or get an instance of TestApplication for that test case */
    val <http://StringSpecScope.app|StringSpecScope.app>: TestApplication
      get() = apps.getOrPut(this.testCase.descriptor) {
        TestApplication(this.testCase.descriptor)
      }
  }