0

I am currently trying to test my Exposed Kotlin code. I have a table that follows the form

object Foo: Table() {
   *parameters*
}

and a method that looks something like

fun addNewFoo(){
    Foo.insert { ... }
}

I'm testing addNewFoo and I want to verify the insert occurred, ideally using something like

verify { FooSpy.insert { ... } } 

How do I mock the Foo table to be a spy so I can verify the call occurred, or what other approach should I take to verify this method being called?

jpthesolver2
  • 1,107
  • 1
  • 12
  • 22

2 Answers2

0

You can first mock your singleton Foo class using mockkObject() and then verify. Here is the code:

mockkObject(Foo) // mock the object
addNewFoo() // call function that we're testing
verify { Foo.insert(any()) } // verify
Natig Babayev
  • 3,128
  • 15
  • 23
  • Thanks! Normally this would work great, but unfortunately since ```Foo``` is an Exposed table, it has some weird properties, and this doesn't work. The ```insert``` needs to be inside a transaction, which makes it harder to test – jpthesolver2 Oct 09 '20 at 15:03
0

There is discussion of ways to go about it: https://github.com/JetBrains/Exposed/issues/317

There seems to be no real intended way for testing but making small test tables in a test data base is the closest you can get.

Steph
  • 831
  • 6
  • 19