Applying Named Setup Functions to Our Tests
Let’s start applying named setup functions to our responses.
We'll cover the following...
With the trade-offs in mind, we will choose to write some named setups to achieve the following:
-
Control duplication.
-
Simplify each test block.
Responses
Let’s start with /test/response_test.exs
since it’s complex enough to need named setups, but simple enough to illustrate the concept.
The first step is to create the file with the basic heading, like this:
Press + to interact
defmodule ResponseTest douse ExUnit.Caseuse QuizBuildersend
We use the QuizBuilders
macro to build in our fixtures.
Next, we’ll build some simple local functions to build a quiz with the exact quiz and templates we need. We’ll fill them in /test/response_test.exs
, like this:
Press + to interact
defp quiz() dofields = template_fields(generators: %{left: [1], right: [2]})build_quiz()|> Quiz.add_template(fields)|> Quiz.select_questionenddefp response(answer) doResponse.new(quiz(), "mathy@example.com", answer)end
-
Since we’re testing for correct responses, we want a repeatable template with only one possible ...