Hi, Big fan of Kotest! Thanks for this amazing lib...
# kotest
n
Hi, Big fan of Kotest! Thanks for this amazing library. Newbie property testing question: I'm trying to compose an object with
Arb.bind()
, the object contains a list of other objects. I'm having trouble generating a list of objects to populate my main arb with.. Here's what I have
Copy code
class CartModelTest : StringSpec({
    "create Cart from CartEntity" {
        val cartItemArb: Arb<CartLineItemEntity> = Arb.bind(
            Arb.string(),
            <http://Arb.int|Arb.int>(1..999)
        ) { randomItemId, randomQuantity ->
            CartLineItemEntityBuilder()
                .itemId(randomItemId)
                .quantity(randomQuantity)
                .build()

        }
        val cartEntityArb: Arb<CartEntity> = Arb.bind(
            Arb.string(),
            Arb.double(),
            <http://Arb.int|Arb.int>()
        ) { randomId, randomAmount, randomItemCount ->
            val amount = AmountBuilder().value(randomAmount).build()
            CartEntityBuilder()
                .cartId(randomId)
                .grandTotal(amount)
                .subtotal(amount)
                .totalDiscount(amount)
                .numberOfItems(randomItemCount)
                    //TODO how do i create a random list of CartLineItems?
                .itemList(listOf(cartItemArb.single()))
                .build()
        }
        checkAll(cartEntityArb) { cartEntity ->
            val result = Cart.fromCartEntity(cartEntity)
            result.workfile shouldBe cartEntity.cartId
        }
    }
})
How do I populate itemList in my
cartEntityArb
with a list of
cartItemArbs
?
s
In your cart entity arb, make a fourth parameter by an Arb.list that itself uses your cart item arb
Copy code
val cartEntityArb: Arb<CartEntity> = Arb.bind(
            Arb.string(),
            Arb.double(),
            Arb.list(cartItemArb, 1..10)
        ) { randomId, randomAmount, randomItems ->
n
Awesome. that worked beautifully. Thank you! 🙌
🙂 1