编写 java.lang.IllegalStateException:尚未应用待处理的组合

时间:2021-07-07 10:46:34

标签: android-jetpack-compose

我只想运行简单的测试

class Exa {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
// createComposeRule() if you don't need access to the activityTestRule

@Test
fun MyTest() {
    // Start the app
    composeTestRule.setContent {
            Greeting2("Nurs")
    }
    
    composeTestRule.onNodeWithText("Hello Nurs!").assertIsDisplayed()
}
}

@Composable
fun Greeting2(name: String) {
 Text(text = "Hello $name!")
}

但它给了我以下错误:

java.lang.IllegalStateException: pending composition has not been applied

奇怪的是,如果我在另一个项目中运行它,它会起作用

1 个答案:

答案 0 :(得分:-2)

问题出在 MainActivity:

class MainActivity3 :
ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
            // A surface container using the 'background' color from the theme
            Surface(color = MaterialTheme.colors.background) {
                Greeting("Android")
            }
    }
}
} 

@Composable
fun Greeting(name: String) 

你必须使用没有任何依赖的 clear Activity