Hi - we're trying to verify that an `onCreateViewH...
# mockk
d
Hi - we're trying to verify that an
onCreateViewHolder
is mapping the
viewType
to the proper
ViewHolder
. The code looks something like
Copy code
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VerticalTabItemBaseViewHolder {
    when (viewType) {
        ViewType.TYPE_A.ordinal -> {
            val binding = ItemTypeABinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
            return ItemViewHolderBase.ItemAViewHolder(binding)
        }

        ViewType.TYPE_B.ordinal -> {
            val binding = ItemTypeBBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
            return ItemViewHolderBase.ItemBViewHolder(binding)
        }
        ...
    }
}
The
ViewHolders
are all within a sealed class that needs an
itemBackground
that is in the binding
Copy code
sealed class ItemViewHolderBase(
    rootView: View,
    val itemBg: View
): RecyclerView.ViewHolder(rootView) {
    class ItemAViewHolder(
        val binding: ItemTypeABinding
    ): ItemViewHolderBase(binding.root,
        binding.itemBg
    )
    class ItemBViewHolder(
        val binding: ItemTypeBBinding
    ): ItemViewHolderBase(binding.root,
        binding.itemBg
    )
}
We're able to get a mock of the binding object, but the contained views are
null
, so when they get consumed by the base class, we get a nullability exception We then tried to have our mock binding return a mock item background with
Copy code
val mockRoot : ConstraintLayout = mockk(relaxed = true)
val mockItemBg : View = mockk(relaxed = true)
every { mockBinding.root } returns mockRoot
every { mockBinding.itemBg } returns mockItemBg
but we get an exception for
itemBg
that says
Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
Can anybody provide insight here?
My suspicion is that it has to do with the
itemBg
being a property rather than a method. I did try to use the syntax referred to in the docs to mock properties, but got the same thing
every {mockBinding getProperty "itemBg"} returns mockItemBg