19

I am having problems testing my app. I created an espresso test which is supposed to fail, since whenever I launch my app in the emulator, I get the expected behavior. There is my test:

 onView(withText("wrong answer")).perform(click());
 onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));

When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is "wrong answer".

Loïs Talagrand
  • 810
  • 2
  • 13
  • 32

1 Answers1

46

According to what I understand, you want it to work like this:

if nextQuestionButton IS enabled, then take following actions:

  • click on 'wrong answer',
  • check if nextQuestionButton changed stated to NOT enabled.

If this is so, the code should be like this:

onView(withId(R.id.nextQuestionButton)).check(matches(isEnabled()));
onView(withText("wrong answer")).perform(click());
onView(withId(R.id.nextQuestionButton)).check(matches(not(isEnabled())));

Espresso allows you to use Hamcrest matchers in tests.

Hamcrest 1.3 Quick Reference.

Please check also this (if you haven't done that already):

Espresso 2.1. Espresso Cheat Sheet Master [updated]

According to this fragment of your post:

When launching the test, nothing is reported, whereas nextQuestionButton should not be enabled upon clicking the radioButton whose text is "wrong answer".

It means that you hadn't set disabled your next question button, so Espresso passes this test.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • 1
    I cant use 'matches(isEnabled())' it says that we require viewAssertion but found boolean!!!!!! i using kotlin – milad salimi Aug 07 '19 at 10:31
  • Cheat Sheet Site not Found! Please update your answer – dudi Feb 24 '20 at 13:41
  • @dudi sorry for inconvenience. Link updated. Anything more you need? :-) – piotrek1543 Mar 05 '20 at 12:04
  • matches(not(isEnabled())) is not working for me. I get these 2 errors: Type mismatch: inferred type is Validator but Matcher! was expected and Type mismatch: inferred type is Matcher! but Validator was expected. How to fix this? – Dominik Nov 10 '20 at 09:34