1

I have a listview in my fragment UI that its elements set depend on status of a value that come from a viewmodel LiveData attribute.

I want to create instrumental test for the fragment which englobes 3 scenarios test case related to the value set of that attribute and I don't where to start.

My code should kind look like below :

class MyViewModel : ViewModel() {
var status = MutableLiveData("")
}


class MyFragment : Fragment() {

private lateinit var myViewModel: MyViewModel

private lateinit var myListView: ListView

override fun onAttach(context: Context) {
    AndroidSupportInjection.inject(this)
    super.onAttach(context)

    myViewModel =
        ViewModelProviders.of(this, ViewModelProvider.Factory).get(MyViewModel::class.java)
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    when (myViewModel?.status) {

        "status1":
            setListContent(items1)

        "status2":
            setListContent(items2)

        "status3":
            setListContent(items3)

        else
            setListContent(items1)

    }
}

private fun setListContent(itemsList: List<?>) {
    myListView.adapter = MyCustomadapter(context!!, itemsList)
}
}
Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44
  • There is a similar issue described here: https://stackoverflow.com/questions/59666993/android-how-to-write-a-unit-test-for-fragment-depending-on-a-viewmodel-live-da – CodeRanger Mar 14 '20 at 13:47

0 Answers0