我正在尝试为我的 Android 应用程序编写一个简单的 UI 测试:
@RunWith(AndroidJUnit4::class)
class SimpleTest {
@get:Rule
val activityRule = activityScenarioRule<MainActivity>()
@Test
fun justPass() {}
}
Run Code Online (Sandbox Code Playgroud)
问题是这个测试永远挂起,并且应用程序卡在这个屏幕上:
它发生在模拟器和真实设备上。完整的标题是androidx.test.core.app.InstrumentationActivityInvoker$EmptyActivity.
奇怪的是,如果我按下方形按钮,然后从最近的应用程序中选择相同的屏幕,测试就会完成。
这些是我的测试依赖项:
testImplementation 'junit:junit:4.13.1'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation 'android.arch.core:core-testing:2.1.0'
testImplementation "io.mockk:mockk:$mockk_version"
androidTestImplementation 'androidx.test:core-ktx:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
Run Code Online (Sandbox Code Playgroud)
如果我添加androidx.test:rules:1.3.0并替换activityScenarioRule为ActivityTestRule,测试将按我的预期自动完成。但 ActivityTestRule 已被弃用,所以我认为这不是一个好的选择。
那么我应该怎么做才能让这个测试通过而不被卡住呢?
MainActivity 在清单中声明,启动模式为单实例:
testImplementation 'junit:junit:4.13.1'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation 'android.arch.core:core-testing:2.1.0'
testImplementation "io.mockk:mockk:$mockk_version"
androidTestImplementation 'androidx.test:core-ktx:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
Run Code Online (Sandbox Code Playgroud)
如果我删除该launchMode属性,测试将不再挂起。不过,应该可以测试具有该属性的活动,因此我仍在寻找解决方案。
我想在我的Android应用程序中自定义警报对话框,所以我首先更改了应用程序主题的alertDialogTheme属性,如下所示:
RES /值/的themes.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"/>
</resources>
Run Code Online (Sandbox Code Playgroud)
只是通过这样做,我没想到对话框看起来与标准不同,因为新主题AlertDialogTheme与其父对象完全相同.它在Android Lollipop上运行正常,但在Android Kitkat中,对话框后面会出现一个白色矩形.这是它的样子:
我做错了吗?也许使用错误的父母?
谢谢!
我有这个视图模型:
class MyViewModel(private val myUseCase: MyUseCase) : ViewModel() {
val stateLiveData = MutableLiveData(State.IDLE)
fun onButtonPressed() {
viewModelScope.launch {
stateLiveData.value = State.LOADING
myUseCase.loadStuff() // Suspend
stateLiveData.value = State.SUCCESS
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个测试来检查状态是否真的LOADING在myUseCase.loadStuff()运行时。我正在使用 MockK。这是测试类:
@ExperimentalCoroutinesApi
class MyViewModelTest {
@get:Rule
val rule = InstantTaskExecutorRule()
private lateinit var myUseCase: MyUseCase
private lateinit var myViewModel: MyViewModel
@Before
fun setup() {
myUseCase = mockkClass(MyUseCase::class)
myViewModel = MyViewModel(myUseCase)
}
@Test
fun `button click should put screen into loading state`() = runBlockingTest {
coEvery …Run Code Online (Sandbox Code Playgroud) 我有返回a Option或a的函数Result:
fn get_my_result() -> Result<(), Box<Error>> {
lots_of_things()?;
Ok(()) // Could this be omitted?
}
fn get_my_option() -> Option<&'static str> {
if some_condition {
return Some("x");
}
if another_condition {
return Some("y");
}
None // Could this be omitted as well?
}
Run Code Online (Sandbox Code Playgroud)
目前,没有Ok(())或None允许被省略,如图上面的例子.这有什么理由吗?将来有可能改变这种情况吗?
let x: i32 = 4;
let y: i16 = 4;
println!("{}", x == y);
Run Code Online (Sandbox Code Playgroud)
编译上面的代码片段时,编译器将显示以下错误:
let x: i32 = 4;
let y: i16 = 4;
println!("{}", x == y);
Run Code Online (Sandbox Code Playgroud)
似乎PartialEq没有为不同类型的整数实现。f32和f64,和之间也发生相同的情况PartialOrd。有什么理由吗?是否打算在Rust的未来版本中实现?