测试RxBinding RxSearchView

Gra*_*ith 16 android android-appcompat searchview rx-java rx-android

背景

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);
}
Run Code Online (Sandbox Code Playgroud)

我试图用AndroidJUnit4跑步者测试上面的方法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);
}
Run Code Online (Sandbox Code Playgroud)

问题

我尝试过使用a mockSearchView和real SearchView.

mockSearchView = mock(SearchView.class);
searchView = new SearchView(InstrumentationRegistry.getContext(), null);
searchView = new SearchView(InstrumentationRegistry.getTargetContext(), null);
Run Code Online (Sandbox Code Playgroud)

在实例化期间,实际对象在测试运行时会导致不同的异常.模拟对象在执行期间似乎没有任何效果.

更新

为清楚起见:理想情况下,如果我可以模拟SearchView,那将是很好的,因为我想测试发生什么后发生的事情以及使用正确的输入调用performSearch方法.

woj*_*ski 1

为了测试 RxBindings,我只是简单地使用了 Espresso 并将其设置debounce为 0(去抖时间作为公共静态,在 espressosetUp()方法中我将其设置为 0)。然后就

onView(withId(yourId)).perform(ViewActions.replaceText(to something))  
Run Code Online (Sandbox Code Playgroud)

只需通过throttleLast()操作员验证您的 Mockito 或类似内容即可。

干杯