Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing RxBinding RxSearchView

Background

public Observable<List<Foo>> search(SearchView searchView) {

    return RxSearchView.queryTextChanges(searchView)
            .filter(charSequence -> !TextUtils.isEmpty(charSequence))
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(200, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .flatMap(this::performSearch) //Search the DB
            .onErrorResumeNext(this::doSomething);
}

I am trying to test the above method using the AndroidJUnit4 runner and Mocktio.

@Test
public void testSearchCallsDataManager_WhenCalled() {

    String input = "abc";

    when(mockSearchView.getQuery()).thenReturn(input);

    searchRequestManager.search(mockSearchView).subscribe(testSubscriber); //Using standard TestSubscriber

    testSubscriber.assertNoErrors();
    testSubscriber.assertNotCompleted();
    verify(mockDataManager).getFoos(input);
}

The Problem

I have tried using a mockSearchView and a real SearchView.

mockSearchView = mock(SearchView.class);
searchView = new SearchView(InstrumentationRegistry.getContext(), null);
searchView = new SearchView(InstrumentationRegistry.getTargetContext(), null);

The real objects result in different exceptions when the test runs, during their instantiation. The mock object seems to have no effect during execution.

Update

For clarity: Ideally it would be great if I could mock the SearchView as I want to test what happens AFTER something is emitted and that the performSearch method is called with the correct inputs.

like image 669
Graham Smith Avatar asked Jul 21 '16 13:07

Graham Smith


1 Answers

For testing RxBindings I just simply used Espresso and set debounce to 0 ( debounce time as public static, and in espresso setUp() method I set it up to 0). Then just

onView(withId(yourId)).perform(ViewActions.replaceText(to something))  

and just verify your Mockito or whatever similar with throttleLast() operator.

Cheers

like image 68
wojciech_maciejewski Avatar answered Nov 16 '22 21:11

wojciech_maciejewski