Testing a Fragment Scenario with ViewBinding

982 Views Asked by At

I'm trying to test a fragment that uses viewBinding to show the views, and a viewModel to fetch that data.

I want to write a UI test to see if certain data is visible or not, but so fare I've had no luck and I'm hoping somebody can help me as I'm still very new to UI testing.

So far I tried mocking the viewModel, the liveData and the viewBinding, but to no success

Here are a few example classes of what I have tried to do

My ViewModel:

   
    private val bike: MutableLiveData<Bike> = MutableLiveData()
  
    val getBike: LiveData<BikeType>
        get() = bikeType


    fun getBike() {
       
            BikeService().getBike(bikeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    bike.postValue(it)
                }, {
                    Timber.e(it)
                }).let { disposeBag.add(it) }
        }
    }
 }

My Fragment

class BikeFragment : AppAbstractBaseFragment<Any>(), BikeView,
    OnBackPressedListener {

    var mBinding: FragmentBikeBinding? = null
    var viewModel: BikeViewModel? = null

    var cancellable: Boolean = false


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = FragmentBikeBinding.inflate(inflater, container, false)
        return mBinding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (viewModel == null) {
            viewModel = ViewModelProvider(this).get(BikeViewModel::class.java)
        }
        mBinding?.apply {
            this.view = this@BikeDashboardFragment
            
            viewModel?.getBike?.observe(viewLifecycleOwner, { newBike ->
                bike = newBike

            })
          
        }
      
        viewModel?.getBike()
    }

The Test

class BikeTest {

    @get:Rule
    val screenshotTestRule = ScreenshotTestRule()

    @Rule
    @JvmField
    val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule()

    @Rule
    @JvmField
    var instantExecutor = InstantTaskExecutorRule()

    private var scenario: FragmentScenario<BikeFragment>? = null
    private var bikeData = mockk<MutableLiveData<Bike>>()

    private var viewModel = mockk<BikeViewModel>(relaxed = true)
    private val lifecycleOwner: LifecycleOwner = mockk()
    private var fakeBike =
        Bike(id = 1, cancellable = true, linkable = true, ownerName = "Joske")

    var context: Context? = null


    @Before
    fun setUp() {
        val app = ApplicationProvider.getApplicationContext<BaseApplication>()
        context = app.applicationContext

    }

    @Test
    fun loadBikeSeeTheRightBikeData() {
        scenario = launchFragmentInContainer(
            themeResId = R.style.Theme_Brand
        )
        dataBindingIdlingResourceRule.monitorFragment(scenario!!)
        scenario?.onFragment {
            it.mBinding?.bike = fakeBike
            onView(withId(R.id.ownerNameTextView)).check(matches(withText("Klaartje")))
        }
    }

}
0

There are 0 best solutions below