fake_server
fake_server copied to clipboard
Providing test context while using `test_with_server`
How can we use test context from ExUnit while using test_with_server instead of test from ExUnit?
For example, we can use this syntax with ExUnit test macro.
test "uses metadata from setup", context do
assert context[:hello] == "world"
assert context[:from_named_setup] == true
end
but we cannot provide a context for
test_with_server as the second parameter is opts.
Posting a workaround for anyone that encounters this problem like we did as well.
Manually start fake server in a setup function and add that to your context.
def start_fake_server(_context) do
name = String.to_atom("test_server_#{System.unique_integer([:positive, :monotonic])}")
FakeServer.start!(name)
on_exit(fn -> FakeServer.stop(name) end)
[fake_server: FakeServer.Instance.state(name)]
end
# ..., usage:
setup :start_fake_server
setup :something_else
test "some test", %{
fake_server: fake_server, something_else: something_else
} do
FakeServer.put_route!(
fake_server.server_name,
ClientRequest.api_path() <> "/xxxx",
FakeServer.Response.ok!(%{"xxxx" => xxxx}, %{"Content-Type" => "application/json"})
)
...
end