unit-testing - 单元测试 RxJava doOnSubscribe 和 doFinally

标签 unit-testing testing rx-java2

我如何创建一个单元测试,以在 rxjava 链的 doOnScubscribedoFinally 上完成某种副作用?

例如:

Observable.requestSomeValueFromWeb()
            .doOnSubscribe(() -> showLoading = true)
            .doFinally(() -> showLoading = false)
            .subscribe(result -> doSomething(result), error -> doErrorHandling(error));

我如何在上面的场景中测试 showLoading 在订阅时设置为 true 而在处理 observable 时设置为 false?

TestSubscriber<WebServiceResponse> loginRequestSubscriber = new TestSubscriber<>();

clientLoginViewModel.requestLogin().subscribe(loginRequestSubscriber);

// check that showLoading was true when webservice was called
assertEquals(true, showLoading);

// check that showLoading was false when webservice was finished
assertEquals(false, showLoading);

loginRequestSubscriber.assertSubscribed();

最佳答案

If I understand correctly, your Object Under Test is the ClientLoginViewModel, so I'm trying to work from there. Let me know if I'm mistaken and I can revisit my answer:

您系统中的类:

interface WebServiceResponse { } // We don't care about this here

interface Network {
    // This is whatever interacts with the Network, and we'll mock it out
    Single<WebServiceResponse> requestSomeValue();
}

// The class we are testing
class ClientLoginViewModel {

    final Network mNetwork;

    // This is the field we want to check... you probably want to make it
    // private and have accessors for it
    boolean showLoading;

    // This allows dependency injection, so we can mock :)
    ClientLoginViewModel(final Network network) {
        mNetwork = network;
    }

    // The actual method to test!
    Single<WebServiceResponse> requestLogin() {
       return mNetwork.requestSomeValue()
           .doOnSubscribe(disposable -> showLoading = true)
           .doFinally(() -> showLoading = false);
    }
}

现在是测试!!!

class ClientLoginViewModelTest {

    @Test
    public void testLoading() {
        final Network network = mock(Network.class);
        final WebServiceResponse response = mock(WebServiceResponse.class);

        // This is the trick! We'll use it to allow us to assert anything
        // before the stream is done
        final PublishSubject<Boolean> delayer = PublishSubject.create();
        when(network.requestSomeValue())
            .thenReturn(
               
 Single.just(response).delaySubscription(publishSubject)
            );

        final ClientLoginViewModel clientLoginViewModel = new ClientLoginViewModel(network);

        clientLoginViewModel
            .requestLogin()
            .test();

        // check that showLoading was true when webservice was called
        assertEquals(true, clientLoginViewModel.showLoading);

        // now let the response from the Network continue
        publishSubject.onComplete();

        // check that showLoading was false when webservice was finished
        assertEquals(false, clientLoginViewModel.showLoading);
    }
}

关于unit-testing - 单元测试 RxJava doOnSubscribe 和 doFinally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48980897/

相关文章:

java - 为什么map被调用多次?

java - 具体类 Mockito 中的模拟链式调用

java - 如何使用PowerMock测试方法Jsoup.connect(静态)?

wpf - 在测试/开发期间替换 ADFS

angular - 从我的组件单元测试中测试服务调用

java - 如何使用 RxJava 2 的 CompositeDisposable?

java - 响应式(Reactive)编程的优点/缺点

ruby-on-rails - rails : test single file with rake test without resetting database

unit-testing - .NET Framework 上的单元测试和 Web 测试?

java - 如何在 Android JUnit 测试中测试来自服务的处理程序回调?