Ivan Varga
01/29/2025, 9:22 AMclass MyViewModel(
private val a: A?,
backgroundCoroutineScope: BackgroundCoroutineScope,
) : ViewModel(viewModelScope = backgroundCoroutineScope.scope)
and a Unit test with Konsist:
Konsist.scopeFromProject()
.classes()
.withParentOf(ViewModel::class)
.withName { it.equals("MyViewModel", ignoreCase = true) }
.withPrimaryConstructor()
.properties()
.print()
The print
statement you see above, prints only a
, but not backgroundCoroutineScope
.
I assume this is due to the backgroundCoroutineScope
not being a var nor a val.
Is there a way I can verify that this constructor property exists?
Thank you 🙏Ivan Varga
01/29/2025, 9:23 AMbackgroundCoroutineScope
is passed in to the ViewModel constructor.Vidmantas Kerbelis
01/29/2025, 2:04 PMKonsist.scopeFromProject()
.classes()
.withParentOf(ViewModel::class)
.withName { it.equals("MyViewModel", ignoreCase = true) }
.mapNotNull { it.primaryConstructor }
.forEach {
println("${it.parameters}")
}
Result: [a, backgroundCoroutineScope]
Vidmantas Kerbelis
01/29/2025, 2:08 PMtext
field of the declaration and parsed + asserted what I needed using String manipulation.Ivan Varga
01/29/2025, 2:09 PMVidmantas Kerbelis
01/29/2025, 2:12 PM) : ViewModel(viewModelScope = backgroundCoroutineScope.scope)
And check appropriately.
Even if you get it working:
) : ViewModel(
viewModelScope = backgroundCoroutineScope.scope)
)
Such formatting becomes an issue immediately.Ivan Varga
01/29/2025, 2:14 PM