Looking at the ExUnit documentation, you can add properties to the context
struct with the following pattern:
defmodule KVTest do
use ExUnit.Case
setup do
{:ok, pid} = KV.start_link
{:ok, pid: pid}
# "[pid: pid]" also appears to work...
end
test "stores key-value pairs", context do
assert KV.put(context[:pid], :hello, :world) == :ok
assert KV.get(context[:pid], :hello) == :world
# "context.pid" also appears to work...
end
end
But when using describe
macro blocks, you're encouraged to use the following form to provide setup functions for your tests:
defmodule UserManagementTest do
use ExUnit.Case, async: true
describe "when user is logged in and is an admin" do
setup [:log_user_in, :set_type_to_admin]
test ...
end
describe "when user is logged in and is a manager" do
setup [:log_user_in, :set_type_to_manager]
test ...
end
defp log_user_in(context) do
# ...
end
end
Which works well, but there's no mention of how to add new properties to the context struct to use in tests, when using the describe
macro and named setups.
So far, I've tried (quick summary):
...
describe "when user is logged in and is a manager" do
setup [:test]
test(context) do
IO.puts("#{ inspect context }") # Comes up as 'nil'
end
end
defp test(context) do
[test: "HALLO"]
end
...
Is it actually possible to manipulate the test suite context when creating setup functions for describe blocks in this way?