https://kotlinlang.org logo
#konsist
Title
# konsist
s

Swapnil Musale

09/06/2023, 9:04 PM
Hi I'm looking for way to improve my test
Copy code
@Test
    fun `ViewModel class should not have 'Context' as parameter`() {
        moduleScope
            .classes()
            .withAllParentsOf(ViewModel::class)
            .constructors
            .assert {
                it.hasParameterNamed("context")
            }
    }
My test is to ViewModel should not directly receive context as parameter for this I have written above test which work but devs can write any name like context, appContext, mContext. I wanted to know how can I check parameter Type i.e does constructor has any parameter of Context type
i

igor.wojda

09/06/2023, 11:20 PM
Try this
Copy code
.assert {
    it.parameters.none { parameter -> 
        parameter.representsTypeOf<Context>() 
     }
}
1
s

Swapnil Musale

09/07/2023, 4:50 AM
Thanks
2 Views