Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RxJava 2:行为主体和可观察的。组合相关问题_Java_Android_Unit Testing_Rx Java_Rx Java2 - Fatal编程技术网

RxJava 2:行为主体和可观察的。组合相关问题

RxJava 2:行为主体和可观察的。组合相关问题,java,android,unit-testing,rx-java,rx-java2,Java,Android,Unit Testing,Rx Java,Rx Java2,我在组合行为主体和可观察。组合相关测试时遇到了一些问题。我能够在一个(较小的)测试中复制它。以下是一个当前未通过的测试: import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import io.reactivex.Observable; import io.reactivex.observers.TestObserver; import io.re

我在组合
行为主体
可观察。组合相关测试
时遇到了一些问题。我能够在一个(较小的)测试中复制它。以下是一个当前未通过的测试:

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

import static java.util.Collections.singletonList;

public class MyTest {

    @Test
    public void test() {
        Subject<Integer> intSource = BehaviorSubject.createDefault(1);

        Subject<List<Observable<Integer>>> mainSubject = 
            BehaviorSubject.createDefault(singletonList(intSource));

        TestObserver<List<Integer>> testObserver = 
            mainSubject.flatMap(observables ->
                Observable.combineLatest(observables, this::castObjectsToInts)
            )
            .test();

        List<Observable<Integer>> newValue = new ArrayList<>();
        newValue.add(intSource); // same value as before
        newValue.add(Observable.just(2)); // add another value to this list.

        mainSubject.onNext(newValue);

        // intSource was already '1', but this is just to 'update' it.
        intSource.onNext(1); // COMMENT OUT THIS LINE

        testObserver.assertValueAt(0, singletonList(1));
        testObserver.assertValueAt(1, Arrays.asList(1, 2));
        testObserver.assertValueAt(2, Arrays.asList(1, 2)); // COMMENT OUT THIS LINE
        testObserver.assertValueCount(3); // REPLACE 3 WITH 2
    }

    private List<Integer> castObjectsToInts(Object[] objects) {
        List<Integer> ints = new ArrayList<>(objects.length);
        for (Object object : objects) {
            ints.add((Integer) object);
        }
        return ints;
    }
}
import org.junit.Test;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入io.reactivex.Observable;
导入io.reactivex.observer.TestObserver;
导入io.reactivex.subjects.BehaviorSubject;
导入io.reactivex.subjects.Subject;
导入静态java.util.Collections.singletonList;
公共类MyTest{
@试验
公开无效测试(){
Subject intSource=BehaviorSubject.createDefault(1);
主题main主题=
createDefault(singletonList(intSource));
TestObserver TestObserver=
mainSubject.flatMap(可观测->
Observable.CombineTest(Observable,this::castObjectsToInts)
)
.test();
List newValue=newarraylist();
newValue.add(intSource);//与前面的值相同
newValue.add(Observable.just(2));//在此列表中添加另一个值。
main subject.onNext(newValue);
//intSource已经是“1”,但这只是为了“更新”它。
intSource.onNext(1);//注释掉这一行
testObserver.assertValueAt(0,singletonList(1));
testObserver.assertValueAt(1,Arrays.asList(1,2));
testObserver.assertValueAt(2,Arrays.asList(1,2));//注释掉这一行
testObserver.assertValueCount(3);//将3替换为2
}
私有列表castObjectsToInts(对象[]对象){
List ints=新的ArrayList(objects.length);
用于(对象:对象){
添加((整数)对象);
}
返回整数;
}
}
(如果您用“注释掉这一行”注释掉两行,并用
2
替换最后的
3
,则测试成功。)


为什么这个测试失败了?我看不出代码有任何问题。

它失败了,因为您忘记了
main subject.flatMap
仍然有第一个
intSource
处于活动状态,因此
intSource.onNext(1)
将首先触发
combineLatest
序列。
testObserver.assertValueAt(2)
将是
列表中的单个值
assertValueAt(3)
将包含
1,2
,整个序列有4项。

为什么
main subject.flatMap
仍然是自我用
newValue
替换
intSource
以来第一个
intSource
激活的
?测试反映了我希望看到的内容,因此我需要更改代码以反映测试结果。
flatMap
合并源代码,直到所有源代码都完成
switchMap
将“替换”旧流。非常感谢!这就解决了问题。你的建议很有帮助。