Espresso Test Fails Due To Data Binding
This is my viewmodel class : class MainViewModel( private val schedulerProvider: BaseSchedulerProvider, private val api : StorytelService ) : BaseViewModel() { private
Solution 1:
It should be possible with Idling Resource
however they are a little bit tedious.
I've just updated an old viewMatcher code:
/**
* Perform action of waiting for a specific view id to be displayed.
* @param viewId The id of the view to wait for.
* @param millis The timeout of until when to wait for.
*/publicstatic ViewAction waitDisplayed(finalint viewId, finallong millis) {
returnnewViewAction() {
@Overridepublic Matcher<View> getConstraints() {
return isRoot();
}
@Overridepublic String getDescription() {
return"wait for a specific view with id <" + viewId + "> has been displayed during " + millis + " millis.";
}
@Overridepublicvoidperform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
finallongstartTime= System.currentTimeMillis();
finallongendTime= startTime + millis;
final Matcher<View> matchId = withId(viewId);
final Matcher<View> matchDisplayed = isDisplayed();
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
if (matchId.matches(child) && matchDisplayed.matches(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happensthrownewPerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(newTimeoutException())
.build();
}
};
}
then you should only do:
@TestfunshouldBeAbleToLoadList() {
onView(isRoot()).perform(waitDisplayed(R.id.recycler_view, 5000));
}
the 5000 is a timeout of 5 secs (5000 millis), you can change it if you want to.
After waitDisplayed
is executed it could happen that the element is shown or the timeout has been reached. In the last case an Exception
will be thrown.
Solution 2:
You will need to create an Idling Resource
for the binding.
You can check Android Architecture Components sample which have a similar implementation. Here's what you will need to look for:
- Firstly, you will need to add an
Idling Resource
class which checks if there are any bindings pending (you can find an implementation here) - Now you can create a rule which will automatically register/unregister
Idling Resource
for you (you can find an implementation here). - And now you can add this rule to your test and check if it works (sample test implementation you can find here).
Post a Comment for "Espresso Test Fails Due To Data Binding"