Thread
#compose
    Alexa_Gal

    Alexa_Gal

    1 year ago
    Hi guys 👩‍💻 looking for some testing advices i want to create my first
    androidTest
    and im having some problems trying to mock the ViewController of my view, maybe im doing something working but i would like to test only the view without the view controller (i already have the Unit test for my view controller) how would you do it using hilt and compose-navigation (
    HiltAndroidTest
    )
    Peter Mandeljc

    Peter Mandeljc

    1 year ago
    I usually make another composable, that doesn't require controller as a parameter. For example
    fun LoginScreen(vm: LoginViewModel = hiltViewModel(), onPopBack: () -> Unit) {
        val state by vm.anyState.collectAsState()
        LoginView(state, onPopBack)
    }
    
    // Test this
    fun LoginView(anyStateFromVM: ?, onPopBack: () -> Unit) {
        // ...
    }
    Alexa_Gal

    Alexa_Gal

    1 year ago
    would you also pass the functions and state needed on the view e.g
    doLogin: vm.doLogin...
    ?
    Peter Mandeljc

    Peter Mandeljc

    1 year ago
    Yeah, pass all the state, that is provided by VM and expose all callbacks that call VM functions
    LoginView(state, onPopBack, onLogin: vm::onLogin)
    Alexa_Gal

    Alexa_Gal

    1 year ago
    nice, ill try to implement something similar thanks peter 🙂
    Peter Mandeljc

    Peter Mandeljc

    1 year ago
    But you still miss a bit of test coverage, if you test it without VM. In my example LoginScreen isn't tested
    Alexa_Gal

    Alexa_Gal

    1 year ago
    i have a unit test for the VM that one covers the logic there
    🤔
    Peter Mandeljc

    Peter Mandeljc

    1 year ago
    tough you can always make an instance of LoginViewModel, by simply call constructor
    LoginViewModel()
    , you don't need to call
    hiltViewModel<LoginViewModel>()
    Alexa_Gal

    Alexa_Gal

    1 year ago
    that could be a bit more complex since i have @inject props coming from hilt i would need to create one VM with mock props 🤔 dont you think?
    Peter Mandeljc

    Peter Mandeljc

    1 year ago
    yeah, exactly