Reputation: 899
I'm trying to test a mapper function between my UI layer and my Domain layer.
But I get an exception thrown of java.lang.IllegalStateException: Not in a frame
This exception goes away if I change my UIModel to have a val
instead of a var
but then it is useless with the model updates that Jetpack Compose offers.
Is there a way to test this type of mapper?
import androidx.compose.Model
import org.junit.Assert.assertEquals
import org.junit.Test
class DataModelMapperTest {
@Test
fun `data model to ui model`() {
val model = DataModel(5)
val uiModel = UIModel(5)
assertEquals(uiModel, model.toUIModel())
}
@Test
fun `ui model to data model`() {
val model = DataModel(5)
val uiModel = UIModel(5)
assertEquals(model, uiModel.toDataModel())
}
}
@Model
data class UIModel(var value: Int)
data class DataModel(val value: Int)
fun DataModel.toUIModel(): UIModel = UIModel(this.value)
fun UIModel.toDataModel(): DataModel = DataModel(this.value)
Upvotes: 2
Views: 1075
Reputation: 26
I was hitting the same issue. Here is what I figured out: I have to initialize my model objects that is annotated with @Model
within (or maybe after) call to setContent()
.
Google's JetNews sample comes with ComposeTestRule.launchJetNewsApp()
function which is called by JetnewsUiTest.setUp()
. Their test class comes with full use of androidx.ui:ui-test stuff including createComposeRule()
. You might want to have a look at it.
Upvotes: 1