测试RxJava BehaviorProcessor是否发出了一个值。

问题描述 投票:2回答:1

我很难理解为什么所有的处理器都能通过测试,但是 Behavior 没有。

package com.example;

import org.junit.Test;

import io.reactivex.Flowable;
import io.reactivex.processors.*;

public class ProcessorTest {
    private static Flowable<String> justFoo() {
        return Flowable.just("foo");
    }

    private static FlowableProcessor<String> subscribeToFoo(
            FlowableProcessor<String> processor) {
        justFoo().subscribe(processor);
        return processor;
    }

    @Test public void flowable() {  // pass
        justFoo().test().assertValue("foo");
    }

    @Test public void async() {  // pass
        subscribeToFoo(AsyncProcessor.create()).test().assertValue("foo");
    }

    @Test public void replay() {  // pass
        subscribeToFoo(ReplayProcessor.create()).test().assertValue("foo");
    }

    @Test public void unicast() {  // pass
        subscribeToFoo(UnicastProcessor.create()).test().assertValue("foo");
    }

    @Test public void behaviorFail() {  // fail
        subscribeToFoo(BehaviorProcessor.create()).test().assertValue("foo");
    }

    @Test public void behaviorPassing() {  // pass
        subscribeToFoo(BehaviorProcessor.create())
                .test()
                .assertNoValues()
                .assertSubscribed()
                .assertComplete()
                .assertNoErrors()
                .assertNoTimeout()
                .assertTerminated();
    }
}

文件上说 BehaviorProcessor 是一个。

处理者将最近观察到的项目以及所有后续观察到的项目 发送给每个订阅者。

所以在我的理解中,它应该通过 behaviorFail 检验 behaviorPassing. 怎么会这样?

我怎么会写一个有效的测试,知道一个。BehaviorProcessor 发出了某个值?

java unit-testing junit4 rx-java2
1个回答
0
投票

摆脱传递给处理器的终端事件会有帮助。

@Test public void behavior() {
    final BehaviorProcessor<String> processor = BehaviorProcessor.create();
    justFoo().concatWith(Flowable.never()).subscribe(processor);
    processor.test().assertValue("foo");
}
© www.soinside.com 2019 - 2024. All rights reserved.