Thanks for the color on :persistent
. Dropping the option doesn’t fix the issue unfortunately.
The test sends a webhook json message to my phoenix api endpoint. The controller passes the message to a dispatcher, which makes an HTTP request to an external server to fetch historical data. This data from the HTTP response is then loaded into my database. Each application, :dispatch
, :datastore
, etc. has mocks in my test configurations for my unit tests. They are configured in the modules as follows (in accordance with Elixir best practices):
@dispatch = Application.get_env(:dispatch, Adapters)
@dispatch[:router].handle_message(...)
I have an IO.inspect
in the module which prints the value of @dispatch
when the code is run during the test.
In my test app TestSuite
I have the following case:
defmodule TestSuite.TestCase do
use ExUnit.CaseTemplate
using do
quote do
import TestSuite.WebHelper
import TestSuite.RequestHelper
import TestSuite.ConfigHelper
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Datastore.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Datastore.Repo, {:shared, self()})
end
TestSuite.WebHelper.launch_web_server()
TestSuite.ConfigHelper.put_dev_adapters()
:ok
end
end
This case is used in the following test:
defmodule TestSuite.Integration.WebhookTest do
@moduledoc false
use TestSuite.TestCase
alias Datastore.Transaction
@moduletag :integration
describe "webhook" do
test "webhook triggers historical data load" do
IO.inspect Application.get_env(:dispatch, Adapters) # Check configuration when test is run.
webhook = %{
access_token: "test_token",
code: 1,
total_transactions: 16,
message: "Initial transaction pull finished"
}
send_webhook(webhook)
assert {:ok, {count, _trans}} = Transaction.get(100)
assert count == 16
end
end
end
My test result output is the following. You can see that the value of the configuration printed at the start of the test shows the desired config, but when printed from inside the module run during the test, it reflects the config in the test.exs
file which I was hoping to overwrite.
==> test_suite
Including tags: [:integration]
Excluding tags: [:test]
[router: Dispatch.Router.Live] # <- Value from IO.inspect at start of test
[router: Dispatch.Router.Mock] # <- Value from IO.inspect inside module used in test
1) test webhook webhook triggers historical data load (TestSuite.Integration.WebhookTest)
test/integration/webhook_test.exs:11
Assertion with == failed
code: count == 16
lhs: 0
rhs: 16
stacktrace:
test/integration/webhook_test.exs:23: (test)
Finished in 0.3 seconds
1 test, 1 failure