RxJava2将一个可观察对象延迟到另一个可观察对象发出之前

时间:2018-11-22 20:54:47

标签: observable delay rx-java2

这是用于单元测试的目的,因为我想验证一个函数的正确行为,其中两个数据源可能以不同的顺序返回。当前,在我的单元测试中,我正在使用虚拟数据创建一个Observable,并简单地使用delay运算符为我想返回的第二个数据源添加10毫秒的延迟。但是,这似乎有点难看,并且随着我进行越来越多的测试,可能会出现问题。

有没有办法让Observable等待发射,直到另一个Observable完成?

编辑:这是我进行的单元测试的示例

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="78dp"
        android:layout_height="22dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="8dp"
        android:text="Fixed"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/radioButton3"
        app:layout_constraintTop_toBottomOf="@+id/my_spinner"
        app:layout_constraintVertical_bias="0.076" />

    <ImageView
        android:id="@+id/faceImage"
        android:layout_width="91dp"
        android:layout_height="66dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.038"
        app:srcCompat="@drawable/common_google_signin_btn_icon_dark" />

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.577" />

    <TextView
        android:id="@+id/insured"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_marginBottom="64dp"
        android:layout_marginStart="76dp"
        android:text="Insured"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:background="@color/colorAccent"
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="SAVE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/ageTextView"
        android:layout_width="37dp"
        android:layout_height="24dp"
        android:layout_marginBottom="116dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:text="Age"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.047"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.98" />

    <TextView
        android:id="@+id/dollarTextView"
        android:layout_width="wrap_content"
        android:layout_height="21dp"
        android:layout_marginBottom="124dp"
        android:layout_marginEnd="104dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Dollar Value"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioButton3"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Rate"
        android:textAlignment="center"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/insured"
        app:layout_constraintEnd_toStartOf="@+id/radioButton4"
        app:layout_constraintHorizontal_bias="0.621"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_spinner"
        app:layout_constraintVertical_bias="0.189" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="81dp"
        android:layout_height="19dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="52dp"
        android:layout_marginTop="8dp"
        android:text="Hourly"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_spinner"
        app:layout_constraintVertical_bias="0.07999998" />

    <EditText
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="First Name"
        app:layout_constraintBottom_toTopOf="@+id/lastName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/faceImage" />

    <EditText
        android:id="@+id/lastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Last Name"
        app:layout_constraintBottom_toTopOf="@+id/Address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.948" />

    <EditText
        android:id="@+id/Address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Address"
        app:layout_constraintBottom_toTopOf="@+id/my_spinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent" />

    <RadioButton
        android:id="@+id/yesRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="YES"
        app:layout_constraintBottom_toTopOf="@+id/saveButton"
        app:layout_constraintEnd_toStartOf="@+id/noRadioButton"
        app:layout_constraintHorizontal_bias="0.513"
        app:layout_constraintStart_toEndOf="@+id/insured"
        app:layout_constraintTop_toBottomOf="@+id/dollarTextView"
        app:layout_constraintVertical_bias="0.894" />

    <RadioButton
        android:id="@+id/noRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_marginBottom="68dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="NO"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.516"
        app:layout_constraintStart_toEndOf="@+id/insured" />

    <EditText
        android:id="@+id/ageEditText"
        android:layout_width="94dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/dollarTextView"
        app:layout_constraintHorizontal_bias="0.81"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioButton4"
        app:layout_constraintVertical_bias="0.088" />

    <EditText
        android:id="@+id/dollarEditText"
        android:layout_width="64dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintStart_toEndOf="@+id/dollarTextView"
        app:layout_constraintTop_toBottomOf="@+id/radioButton3"
        app:layout_constraintVertical_bias="0.088" />

</android.support.constraint.ConstraintLayout>

我正在对数据库返回的可观察值使用public void testNetworkReturningBeforeLocal() { Item localItem = new Item("local"); Item remoteItem = new Item("remote"); when(mDatabaseMock.getItem(any())).thenReturn(Observable.just(localItem).delay(10, TimeUnit.MILLISECONDS); when(mApiMock.getItem(any())).thenReturn(Observable.just(remoteItem)); TestObserver<Item> observer = new TestObserver<>(); mRepository.getItem("blah").subscribe(observer); // calls getItem from database and API observer.awaitTerminalEvent(); observer.assertResult(remoteItem); } 来模拟首先到达的网络响应。我想做的是将数据库的可观察性延迟到网络可观察的完成为止。

0 个答案:

没有答案