Marcello Galhardo

  • 0 Posts
  • 7 Comments
Joined 1 year ago
cake
Cake day: June 10th, 2023

help-circle




  • Anyone else used this kind of setup?

    No.

    Are there any additional tricks I’m missing?

    Depending on your testing framework, if your test class is not recreated, the lazy may hold an old and obsolete value. Apart from that, I don’t see any issue.


    My personal opinion: I find a little odd to use lazy like that - I would personally use data classes for these entities, create constants if they are reused and called them directly passing the data class (similar to your first version).

    data class Author(...)
    data class Book(...)
    
    fun insertAuthor(value: Author): Long { ... }
    fun insertBook(value: Book): Long { ... }
    
    fun testCheckOut() {
        val authorID = insertAuthor(CANNED_AUTHOR)
        val bookID = insertBook(CANNED_BOOK, authorId)
        
        library.checkOut(bookID)
        // ...then assert the book is checked out
    }
    
    // Companion or top level...
    companion object {
      val CANNED_AUTHOR = Author(...)
      val CANNED_BOOK = Book(...)
    }