Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing test context while using test_with_server #55

Open
faizann opened this issue Oct 24, 2022 · 1 comment
Open

Providing test context while using test_with_server #55

faizann opened this issue Oct 24, 2022 · 1 comment

Comments

@faizann
Copy link

faizann commented Oct 24, 2022

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.

@walter
Copy link

walter commented Jan 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants