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

mox test documentation issue #102

Open
stocks29 opened this issue May 7, 2024 · 2 comments
Open

mox test documentation issue #102

stocks29 opened this issue May 7, 2024 · 2 comments

Comments

@stocks29
Copy link

stocks29 commented May 7, 2024

when following the test instructions the application fails to start during mix test because the child_spec stub is not defined yet because test_helper.exs runs after the application starts and therefore the cluster attempts to start.

** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyApp.Cluster
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function MyApp.HTTPClientMock.child_spec/1 is undefined (module MyApp.HTTPClientMock is not available)
            MyApp.HTTPClientMock.child_spec([cluster: MyApp.Cluster, url: "https://localhost:9999", username: "doesnotexist", password: "notarealpassword", http_client_adapter: MyApp.HTTPClientMock])
            (snap 0.10.0) lib/snap/supervisor.ex:45: Snap.Cluster.Supervisor.maybe_initialize_http_client/2
            (snap 0.10.0) lib/snap/supervisor.ex:29: Snap.Cluster.Supervisor.init/1
            (stdlib 5.1) supervisor.erl:330: :supervisor.init/1
            (stdlib 5.1) gen_server.erl:962: :gen_server.init_it/2
            (stdlib 5.1) gen_server.erl:917: :gen_server.init_it/6
            (stdlib 5.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

I've tried using Mox's compile-time requirements but it's not possible to define stubs at compile time.

Am I missing something in this approach?

@stocks29
Copy link
Author

stocks29 commented May 7, 2024

I was able to get this working by defining a proxy module which defines child_spec and then delegates request() calls to the mock module.

# test/support/http_client_mock_proxy.ex
defmodule MyApp.HTTPClientMockProxy do
  @behaviour Snap.HTTPClient

  @impl Snap.HTTPClient
  def child_spec(_config), do: :skip

  @impl Snap.HTTPClient
  def request(cluster, method, url, headers, body, opts) do
    impl().request(cluster, method, url, headers, body, opts)
  end

  defp impl() do
    Application.get_env(:my_app, __MODULE__)[:http_client_adapter]
  end
end

# config/test.exs
config :hydra, MyApp.Cluster, http_client_adapter: MyApp.HTTPClientMockProxy
config :hydra, MyApp.HTTPClientMockProxy, http_client_adapter: MyApp.HTTPClientMock

# test_helper.exs
Mox.defmock(MyApp.HTTPClientMock, for: Snap.HTTPClient)

maybe include something like this in the docs?

@feynmanliang
Copy link
Contributor

+1, I have to override the module definition

defmodule DreamcatcherWeb.HTTPClientMock do
  def child_spec(_), do: :skip
end
config :dreamcatcher, Dreamcatcher.Elasticsearch,
  http_client_adapter: DreamcatcherWeb.HTTPClientMock

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