Testing the Spinner9:11 with Ben Deitch
In this video we'll see how to use 'onData' to match with Views that aren't shown!
We've just finished testing our editText with Espresso, and 0:00 now it's time that we move on to testing our spinner. 0:03 Let's start in the Arrange section, 0:08 by copying over the given color variable from MainActivity test. 0:10 To find the right spinner item to click we'll be using the with text a few so 0:26 let's create a new string variable named spinner item text. 0:31 And set it equal to the string Green. 0:39 In the act section, we need to start by clicking on our spinner. 0:46 Let's type onView(withId( R.id.colorspinner), 0:49 and then, .perform, and for 0:56 the view action, let's pass in click. 1:00 Now that we can see all the items in the spinner, 1:09 we need to click on the one that says Green. 1:11 Lets use control or command D to duplicate this line and 1:15 then replace the withId view matcher with the withText view matcher. 1:18 And instead of passing in an ID, let's pass in our spinnerItemText variable. 1:27 At this point if we run the test again. 1:33 We can see that we're successfully changing the background color to green. 1:45 Nice. 1:50 But before we move on to asserting, we need to talk about an edge case. 1:51 What if we had a spinner with 100 items and we wanted to click on the 98th item. 1:56 Well, the spinner only shows so many items at a time. 2:03 So if we try to click on the 98th item, it's not going to be there. 2:07 And we'll get an error. 2:12 To fix this, 2:14 Espresso gives us the onData method, which we use to replace the onView method. 2:15 OnView only searches through the view hierarchy, but 2:21 onData searches through everything. 2:25 So if you want to click on that 98th item, onData is the way to go. 2:28 Let's refactor our spinnerItem click to use onData instead. 2:32 Then let's copy in a few other imports we'll be needing from the teachers' 2:41 notes below. 2:45 And paste them up here in the import section. 2:47 Next we need to provide an object matcher to match the object we're trying to click. 2:53 So get rid of what's inside our onData method now. 2:59 Because instead of a view matcher, we need to be providing an object matcher. 3:05 So inside the parentheses. 3:09 Let's first use the all of matcher. 3:11 The all of matcher takes in other matchers as parameters and if all 3:17 of those other matchers report a match the all of matcher will report a match too. 3:23 For the first parameter of all of method, 3:30 lets pass on (is(instance0f(String.class). 3:35 We don't want to try and 3:41 match our spinner item text to something that isn't a string, then for 3:43 the second matcher parameter lets pass in is(spinner item text). 3:49 Now it will search through the data in our app looking for 3:56 a string equal to our spinner item text and 4:00 when it finds it, it will find the associated view and perform a click. 4:04 Let's run our test again to make sure we're still changing the background color. 4:11 Perfect. 4:24 Moving onto the assert section we need to assert 4:25 that the background color of the app is equal to the given color. 4:28 Let's grab the linear layout by using the withIdViewMatcher. 4:33 OnView (withId of (R.Id.LinearLayout). 4:40 Then since were asserting we need to add the check method, and now we just need 4:49 to matcher to check that the background of our view matches the given color. 4:55 Unfortunately there isn't a matcher for this. 5:02 So we'll just have to make our own. 5:05 Let's add a line above this one and declare a new bounded matcher variable. 5:08 Named background color matcher. 5:14 And let's set it equal to new bounded matcher. 5:23 Bounded matchers give us a way to match only on objects of a given class. 5:29 But autocomplete didn't fill this part in for us. 5:34 So first, in the matchesSafely method, let's replace object item with 5:38 (LinearLayout linearLayout). 5:44 Then between BoundedMatcher and the parentheses, let's add angle brackets and 5:51 inside lets type view on the linear layout. 5:57 And import view. 6:05 This tells our matcher to only be looking at views and 6:08 then to only match on linear layouts. 6:12 To finish off the errors, 6:16 we just need to provide the class we're looking for as a parameter. 6:18 Linearlayout.class and add a semicolon. 6:23 Finally we just need to return whether or 6:29 not we have a match in the matchesSafely method. 6:32 To check if we have a match, 6:35 let's start by copying over the actual color variable from MainActivityTest. 6:37 And pasting it into our matchesSafely method. 6:46 Then let's delete Activity to leave our linear layout parameter, 6:53 And let's finish up by returning the outcome of givenColor == actualColor. 7:01 The describeTo method is what we'll see in an error message 7:10 if something doesn't match. 7:13 And while we don't have to fill it in, it's quick and it doesn't hurt. 7:15 Let's type description.appendText and 7:19 then for the text let's pass in 7:25 background color should equal: and 7:29 then outside of the quotes. 7:35 Plus given color. 7:39 Semi colon. 7:42 All right. 7:46 We finally finished our custom matcher all that's left is to use it. 7:47 Inside the check method. 7:52 Let's use the matches view assertion. 7:53 And pass in our background color matcher. 7:55 Then let's use Alt+enter to fix this warning. 8:03 And it looks like Android Studio just wanted us to specify the types 8:06 over here as well. 8:11 Okay moment of truth. 8:14 Let's run the test and see what happens. 8:17 All right. 8:30 You're really getting good at this stuff. 8:32 Now, let's comment out the act lines. 8:33 And if we run the test again we should be able to see our description. 8:40 There it is. 8:54 Background color should equal about negative seventeen million. 8:55 AKA color dark green. 9:01 Now let's un-comment the act section and 9:02 then let's conquer our final foe and the next video. 9:06
You need to sign up for Treehouse in order to download course files.Sign up