https://kotlinlang.org logo
#mockk
Title
# mockk
v

Vivek Modi

06/19/2022, 11:47 AM
Hell guys, someone help me how can I verify my extension function. I tried some piece of code but it cannot find the extension function inside my test This function I want to test.
Copy code
internal fun setupExploreConsultationVisibility(hasFocus: Boolean) {
    if (hasFocus) {
       .....
    } else if (viewModel.queryText.isEmpty()) {
        binding.consultationViewSwitcher.displayConsultationViewSwitcherChild(0)
    }
}

internal fun ViewSwitcher.displayConsultationViewSwitcherChild(childNumber: Int) {
        visible()
        displayedChild = childNumber
}
Copy code
@Test
fun `setupExploreConsultationVisibility - `() {
    // STUBBING
    mockViewModel.queryText = ""
    every { mockViewModel.topicSelected } returns ConsultationTopicsArea.ALL
    with(mockConsultationViewSwitcher){
        any<ViewSwitcher>().displayConsultationViewSwitcherChild(0)
    }

    // EXECUTION
    subject.setupExploreConsultationVisibility(false)

    // VERIFICATION
    verify {
        mockViewModel.filterBy(ConsultationTopicsArea.ALL)
    }
}
Copy code
@MockK private lateinit var mockConsultationViewSwitcher: ViewSwitcher

every { mockRootView.findViewById<ViewSwitcher>(R.id.consultationViewSwitcher) } returns mockConsultationViewSwitcher
v

Vaios Tsitsonis

06/27/2022, 12:17 PM
Are test and the function under test in the same package? 🤔
v

Vivek Modi

06/27/2022, 12:18 PM
Yes
2 Views