Tests Call the API as a User Would—Helper Functions
Let’s add some helper functions which will aid in testing our quizzes.
We'll cover the following...
Starting and taking a quiz
Now, we can move on to the helpers that will let us build, start, and take the quiz:
Press + to interact
defp start_quiz(fields) donow = DateTime.utc_now()ending = DateTime.add(now, 60)Mastery.schedule_quiz(Math.quiz_fields(), fields, now, ending)enddefp take_quiz(email) doMastery.take_quiz(Math.quiz.title, email)end
-
start_quiz
schedules the quiz to start immediately with more than enough time to take the quiz. We’ll achieve mastery far before the timer runs out. -
take_quiz
lets a user establish a session.
Answering a quiz
Now, we can move on to the helpers to answer a quiz:
Press + to interact
defp select_question(session) doassert Mastery.select_question(session) == "1 + 2"enddefp give_wrong_answer(session) doMastery.answer_question(session,"wrong", &MasteryPersistence.record_response/2)enddefp give_right_answer(session) doMastery.answer_question(session,"3", &MasteryPersistence.record_response/2)end
By now, these functions should be pretty familiar. We have a function to select a question and then a couple of functions to provide right and wrong answers. Notice that we provide the persistence function to use for saving the responses. That way, only the tests that use these functions ...