Testing Our Quiz

Start testing our quizzes now.

We'll cover the following...

It’s finally time to write some tests. We’ll add these tests at the top of the file after the use directives in /test/quiz_test.exs.

Random questions

First, we need to make sure we’re generating random questions. Here’s the approach:

describe "when a quiz has two templates" do
setup [:quiz]
test "the next question is randomly selected",
%{quiz: quiz} do %{current_question: %{template: first_template}} =
Quiz.select_question(quiz)
other_template = eventually_pick_other_template(quiz, first_template)
assert first_template != other_template
end

This is the first of two tests that use named setups to hide the complexity of data creation by creating our quiz outside of the test block.

  • Our tests are primed with the correct templates, and we have a function that will take the same template and keep generating questions with that same template until it eventually finds a question that doesn’t match ...