Leave a rating/review
We also will need to know if a question was answered correctly. So now we’re going to make the answere method return a boolean. The result will be true when the answer is correct and false when the answer is incorrect.
We’re going to add one more test:
@Test
fun whenAnswering_withCorrectOption_shouldReturnTrue(){
val question = Question("CORRECT", "INCORRECT")
val result = question.answer("CORRECT")
Assert.assertTrue(result)
}
Executing this test will get you a compilation error because the answer method doesn’t return a boolean so go back:
fun answer(option: String): Boolean{
answeredOption = option
return false
}
This will make your test compile for now. Run it and watch it fail.
We’re going to temporary fix this method to return true to make our test pass. Nice.
So, we’re going to do the opposite we’re going to verify that when answering incorrectly we return a boolean equals to false. Create a new test:
@Test
fun whenAnswering_withInCorrectOption_shouldReturnTrue(){
val question = Question("CORRECT", "INCORRECT")
val result = question.answer("CORRECT")
Assert.assertTrue(result)
}
We have the same question object here, it is the same as the one right here. The difference is that we’re now assigning the result variable to the question return value of INCORRECT which should be false. Execute your test, see it fail.
And now to make both of our tests pass go back. And in here instead of just returning a true or false value we’re going to return correctOption == answeredOption
Execute all of your tests ans see them pass. Four out of four. Amazing!!!