How to mock a constructor with a spy?

884 views Asked by At

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?

2

There are 2 answers

1
Natig Babayev On

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
0
Steph On

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.