PhoenixFlags.Testing (PhoenixFlags v0.9.0)

Copy Markdown View Source

Test helpers for PhoenixFlags.

Process-Scoped Stubs

stub/3 stores a value in the process dictionary, scoped to the calling process. When cache_enabled: false (test env), get/3 checks the process dictionary before falling back to the database.

This avoids DB writes and race conditions in async tests:

setup do
  MyApp.SystemConfig.Test.stub("enable_benefits", true)
  :ok
end

Cross-Process (LiveView) Tests

For LiveView and integration tests where the config is read in a different process, insert a DB row instead. The Ecto sandbox in shared mode handles isolation:

PhoenixFlags.Testing.insert_entry(MyApp.Repo, "enable_benefits", true)

Summary

Functions

Reads a per-process stub. Returns {:ok, value} or :none.

Inserts or updates a system flag entry in the database.

Sets a per-process config stub. Only effective when cache_enabled: false.

Functions

get_stub(instance, key)

Reads a per-process stub. Returns {:ok, value} or :none.

insert_entry(repo, key, value, opts \\ [])

Inserts or updates a system flag entry in the database.

Use this for LiveView/integration tests where the config is read in a different process. The Ecto sandbox in shared mode ensures isolation.

PhoenixFlags.Testing.insert_entry(MyApp.Repo, "enable_benefits", true)
PhoenixFlags.Testing.insert_entry(MyApp.Repo, "max_retries", 5, type: "integer")

stub(instance, key, value)

Sets a per-process config stub. Only effective when cache_enabled: false.