Search⌘ K
AI Features

Using Fixture Functions Directly

Explore how to use fixture functions directly to streamline testing of Elixir templates. Understand how to set up tests efficiently, build templates with default and custom data, and verify template compilation for reliable test coverage.

Testing our templates

We have a single test we’ll need for templates. We want to make sure templates get compiled correctly. That test would be difficult if we had to do all of the setup work in the test block itself. Instead, we’ll use the helper functions in QuizBuilders to churn our test out quickly. We’ll create a new file template_test.exs in the test folder and add our latest test, like this:

C++
defmodule TemplateTest do
use ExUnit.Case
use QuizBuilders
test "building compiles the raw template" do
fields = template_fields()
template = Template.new(fields)
assert is_nil(Keyword.get(fields, :compiled))
assert not is_nil(template.compiled)
end
end

Let’s try running some commands to get an idea of how this test works:

Executable

C++
ExUnit.start()
C++
c("test/template_test.exs")
C++
use QuizBuilders
C++
alias Mastery.Core.{Template, Quiz, Response}
C++
fields = template_fields()
C++
template = Template.new(fields)
C++
is_nil(Keyword.get(fields, :compiled))
C++
is_nil(template.compiled)

Output

iex(1)> ExUnit.start()                        
:ok
iex(2)> c("test/template_test.exs")
[TemplateTest]
ie
...